Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am trying to validate the information provided by the user on my website.

I have a textarea to store description of something.
I want to know how can I check if the user only enters numbers, alphabets and common punctuation letters only.

This is how I am checking if the email address entered is valid.
VB
//checks if the email address is a valid email address
              if(!filter_var($_POST['contactInfo'], FILTER_VALIDATE_EMAIL))
Posted

1 solution

Use a regular expression.

if (preg_match(/^[A-Za-z0-9.,+]/, $_POST['your_field']) == 0)
    // text is good


Read up on regular expressions, particularly character classes and escaping. Put the characters you will accept inside the []. In my example, upper- and lower case letters, digits, period comma and plus are acceptable.

If you're having trouble getting it to work, there are online regex tools you can use.

Cheers,
Peter
If this answers your question, mark it accepted. Vote anyway.
 
Share this answer
 
Comments
blink0 4-May-11 15:39pm    
thanks a lot :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900