Okay, the following kills the application if $county is not an alpha character. Now, how can I allow SPACES, for example, the county of "Jersey" would be okay but the county of "New York" keeps getting kicked out. I need both to work.
if (!($county =~ /^[A-Za-z]*$/)) {
&kill_input2;
}
thanks!
I am a bit of hacker when it comes to programming, thought I'd help out though.
So Schoolbag's second expression says any combo of alpha characters followed by a space, any number of times?
In most situations these problems are minor and would have no impact, but there are cases where you need an absolute perfect match, so a more strict approach is necessary:
if($county !~ /\A[[:alpha:]]+(?:[[:space:]]+[[:alpha:]]+)*\z/) { This will only match words containing letters only, optionally with spaces between them, and will not match anything with a trailing space. Also the trailing \z anchor matches the end of the *string*, not the end of the line like $ does, so if there's a newline at the end of the string it will not match.
If you'd prefer not to use the POSIX character classes within the regex, here's a second example that does the same thing:
if($county !~ /\A[A-Za-z]+(?: +[A-Za-z]+)*\z/) { I'm not really a big fan of the above example because of the space in the middle of the regex. Another programmer could come along and totally miss the fact that I put a space there with a "+" quantifier. It would be really easy for this mistake to happen and for them to make an incorrect assumption about what this does.
Here's a third example where I replaced the "hidden" space character with the hexidecimal form of the space character. At least with this example Its more obvious what my intentions are:
if($county !~ /\A[A-Za-z]+(?:\x20+[A-Za-z]+)*\z/) { In production code I would be inclined to use something more explicit like the above example, or the first regex using the POSIX character classes.