DelphiFAQ Home Search:
General :: Programming :: perl
General questions about perl.

Articles:

This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.
Recommended links on this topic:
Featured Article

Check if a user name contains non-alphanumeric characters

Question:

On my web site, a user may put in a desired user name and I have to check that it only contains letters, digits, or the @ and . characters. How can I do that with a regular expression (regex)?

Answer:

The allowed characters are described as A-Za-z0-9.
That are the intervals A-Z, a-z and 0-9. You can also substitute 0-9 with d (digit).

To check for the undesired character, use the ^ operator in front of it as shown below:

if ($username =~ /([^A-Za-z0-9@.])/) {
   print "Problem in user name with '$1'";
 }
 
 

Generated 12:01:56 on Sep 19, 2017