> You are saying - "Report invalid characters unless there are alphabetical
characters in the string".
> But there are alphabetical characters in the string...
> You should have said - "Report invalid characters unless there are ONLY
> alphabetical characters in the string, from the beginning to the end".
>
> That is instead of:
>
> if ($string !~ /[a-zA-Z]/) {
>
> you should have written
>
> if ($string !~ /\A[a-zA-Z]\Z/) {
>
> (\A meaning beginning and \Z the end of the string).

The regex /\A[a-zA-Z]\Z/ will only match when there is a single member of
the character class between the beginning and end of string markers.  You
are saying 'report invalid characters unless there is a single alphabetical
character between the beginning and the end'.  The + operator below means
'match one or more' and applies to the character or character class
immediately preceding it.

if ( $string !~ /\A[a-zA-Z]+\Z/ ) {

Alternatively you could use ^ for the start of the string, and $ for the
end - I prefer these as they involve fewer backslashes e.g.

if ( $string !~ /^[a-zA-Z]+$/ ) {

Best wishes,

Rachel



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to