faqts : Computers : Programming : Languages : PHP : Common Problems : Forms and User Input

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

15 of 21 people (71%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

How can I ensure that data entered by the user is characters A-Z/a-z/0-9 only?

Aug 25th, 2000 16:18
Ben Udall, Tomi Peltola,


If you want to check the input _before_ the form is submitted, you'll 
have to use something like javascript.  However, if the user has 
javascript disabled, it won't work.
The best way to check would be after the user tries to submit the 
form.  Then, you could check the input using regular expresions in PHP.
//Say the user input was stored in $user
if (ereg("[^A-Za-z0-9]", $user))
{
    // The input had bad characters in it
}
else
{
    // The input is ok
}
For more information on regular expressions check out:
http://www.delorie.com/gnu/docs/rx/rx_toc.html