DelphiFAQ Home Search:

Check if a user name contains non-alphanumeric characters

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

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'";
}


Content-type: text/html

Comments:

2014-07-22, 08:22:38
anonymous from United States  
(19TiNa86)

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: