June 17th, 2013, 05:15 PM
-
Email validation
Hi,
i have this code to check to make sure email is entered but don't know how to valid it, can you plz help?
<script language="JavaScript">
function validateForm(oForm) {
if (document.getElementById ("email").value.length == 0) {
alert ("Email must be entered.");
return false;
}
return true;
}
</script>
<form action="process.cfm" method="post" name="form" id="form" onsubmit="return validateForm(this);">
Email<br /> <input type="text" name="email" id="email" size="24" /
</form>
June 17th, 2013, 08:20 PM
-
The code you have is only set-up to check the length of your email input field. It doesn't check to see if it actually contains characters; that would appear in an actual email address. You would need to create a Regular Expression to check for these characters. Take a look at this tutorial from JavaScript Kit About "Advanced Email Verification"; so you will get a basic idea how to do this.
June 17th, 2013, 09:56 PM
-
June 17th, 2013, 11:08 PM
-
Originally Posted by Kravvitz
That will block valid addresses.
What? 
Originally Posted by Description
A script that closely examines the content of a form box to ensure that the user entered a valid email address. If not, the form submition is canceled, and the surfer prompted to re-enter a valid address. The script makes the following assumptions regarding what a valid email address is:
-Contains a least one character procedding the "@"
-Contains a "@" following the procedding character(s)
-Contains at least one character following the "@", followed by a dot (.), followed by either a two character or three character string (a two character country code or the standard three character US code, such as com, edu etc)
Test it out; it validates to see if email address is valid. It does not block valid email addresses; it accepts them and submits form. Otherwise..., if email address is invalid; you get the alert.
That is a good post though; from stackoverflow, maybe somewhat advanced for OP's early JS conceptions, possibly.
Last edited by web_loone08; June 17th, 2013 at 11:17 PM.
June 17th, 2013, 11:18 PM
-
Test it out; it validates, not blocks.
He means that it blocks some valid email addresses, not that it blocks all valid email addresses. For example: "john+doe@gmail.com"
My advice for email validation: just check to see if the string contains at least one @ sign. The point of email syntax validation is to help the user from making a mistake like mixing the fields up and entering their phone number into the field instead of an email address. The point of email syntax validation is not to make them enter a valid email address if they don't want to do it, because you will never succeed at that with a regular expression.
PHP FAQ
Originally Posted by Spad
Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
June 17th, 2013, 11:23 PM
-
Originally Posted by E-Oreo
He means that it blocks some valid email addresses, not that it blocks all valid email addresses. For example: "john+doe@gmail.com"
Oh, ok..., yeah, there are several more advanced versions out there; that try to encapsulate every possible dynamic character scheme, but there is usually a draw back (of some kind) to most all of them. Although, you should always back-up validation on the server-side; I am a pretty big fan of HTML5's built in validation (granted; if browser does not support HTML5, then you need a JS fallback or I guess you could just rely on server-side validation, as a fail-safe method).
Last edited by web_loone08; June 17th, 2013 at 11:31 PM.
June 17th, 2013, 11:24 PM
-
Originally Posted by web_loone08
What?

Test it out; it validates to see if email address is valid. It does not block valid email addresses; it accepts them and submits form. Otherwise..., if email address is invalid; you get the alert.
By "block" I mean it will not allow the form to be submitted when certain valid addresses are used (including ones that I use -- or at least attempt to use -- on a regular basis). More info:
http://isemail.info/about
http://en.wikipedia.org/wiki/Email_address#Address_tags
Since the prose does not fully describe the regex used in the script, it's rather irrelevant.
Originally Posted by web_loone08
That is a good post though; from stackoverflow, maybe somewhat advanced for OP's early JS conceptions, possibly.
Unfortunately, writing good form validation requires a certain skill level.
Comments on this post
June 17th, 2013, 11:39 PM
-
Originally Posted by web_loone08
Oh, ok..., yeah, there are several more advanced versions out there; that try to encapsulate every possible dynamic character scheme, but there is usually a draw back (of some kind) to most all of them.
Some are written to allow any address that is technically valid, however, my point is to not block commonly used address, like the example given by E-Oreo, which is referred to as "plus addressing" by the way. Plus addressing is very useful in conjunction with Gmail's filters. However, many sites block it either through ignorance or due to a few idiot spammers. Though why they can't just do a replace of "\+[^@]+@" with "@" before doing their checks for validity, uniqueness (in their database of users), etc. to avoid the problem, I don't know.
@E-Oreo, do you use plus addressing?
Originally Posted by web_loone08
Although, you should always back-up validation on the server-side; I am a pretty big fan of HTML5's built in validation (granted; if browser does not support HTML5, then you need a JS fallback).
Yeah, the HTML5 form validation is great. I don't consider a JS fallback strictly necessary, but that should be decided on a case-by-case basis.
Originally Posted by E-Oreo
My advice for email validation: just check to see if the string contains at least one @ sign. [...] The point of email syntax validation is not to make them enter a valid email address if they don't want to do it, because you will never succeed at that with a regular expression.
Site owners seem to feel better if users are forced to provide something that's at least syntactically valid.
Last edited by Kravvitz; June 17th, 2013 at 11:44 PM.
June 18th, 2013, 12:45 AM
-
I use it extensively when testing registration systems that require unique email addresses. I rarely use it for filtering, but if I'm programming a script that automatically emails me I'll normally add one.
PHP FAQ
Originally Posted by Spad
Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around