-
email validation
Hi, i was wondering if some1 could tell me the code to validate an email for incorrect charactors.
I believe it is something like this but i am not to sure
PHP Code:
if (ereg('^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$', $address))
return true;
else
return false;
}
also how can i check for certain fields to be field in. i.e my form has 4 text fields ($name,$message,$email,$contact number)
i want name,message and email to be filled in but contact number is optional. i cant figure out how to exclude contact number
Thanks Adam
-
Your e-mail validation expression looks a little strict. The first part of the e-mail address before the @ sign can contain an number of other special characters. Users can enter the email address "[email protected]", which is clearly invalid but not "d^[email protected]", which is valid. You may want to look into more advanced checking of the e-mail address.
This article describes some of them: http://coveryourasp.com/ValidateEmail.asp#Result1
This gives a regular expression e-mail syntax checker. You can see just how many characters are valid in an e-mail address.
http://www-2.cs.cmu.edu/~cache/email/
Checking for optional fields is easy. You just need to check for an empty string in variable. For example the following checks if the field "contact" had anything entered:
PHP Code:
if ($_REQUEST['contact'] == '')
/* nothing entered */
else
/* something was entered */