Use regular expressions for checking user input. They seem complicated at first, but once you understand the methods behind it, it makes alot of sense. If your new to regular expressions, give this (
Regular Expressions Tutorial) a read, it really helps you understand them. Here's my email address verification script.
-
function isEmail (email) {
-
var re =/^[A-Za-z0-9]+[\w.-]*?[A-Za-z0-9]+@[A-Za-z0-9]+[\w.-]*?\.[A-Za-z0-9]{2,5}$/;
-
-
if (email.match(re)) {
-
return true;
-
}
-
return false;
-
}
-
Then to check an email you call it like:
-
if (!isEmail(form.email.value)) {
-
//Things to do if it doesn't match
-
}
-
//things to do if it matches
-
Essentially it checks it like this:
1. Starts with any letter or number (1 or more times)
2. Can be followed by any letter, number, or the symbols "_", "-", or "."
3. Contains any letter number 1 or more times immediately before the "@"
4. Has "@" symbol
5. Checks the same as 1-3 above again after the "@" symbol
6. Ends with a "." followed by 2-5 letters or numbers (.com, .co.uk, etc)
I didn't want email address to be able to start or end with "_" or "." because that is common among spammers, but very rare among standard email users.