Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Email Validation

Status
Not open for further replies.

cdumigan

MIS
Feb 5, 2002
16
0
0
IE
Hi,
I'm looking for an email validation function that will check for '@', '.', and a valid extension (i.e. .com, .uk). I have been able to find some functions on the net, but they all seem to be extremely long and complicated. Does anyone know of an easy way to adequately validate an email field??
Thanks in advance,
C
 
function emailvalidation()
{
str = document.myfrm.email.value;
idxAt = str.indexOf("@");
idxDot = str.indexOf(".");
idxLast = str.length - 1;
if((idxAt) < 1 || (idxDot - idxAt < 2) || (idxLast - idxDot > 3) || (idxLast - idxDot < 2))
{
return false
}
return true;
} ....:::::.... xxLargeWASP ....:::::....
 
This is wha I use:

if (document.formNAME.fieldNAME.value.indexOf(&quot;@&quot;) == -1 || document.formNAME.fieldNAME.value.lastIndexOf('.') == -1) {
alert(&quot;Please include a proper email address. ex: youremailhere@address.com&quot;);
document.formNAME.fieldNAME.value = &quot;&quot;;
document.formNAME.fieldNAME.focus();
return false;
}

What the code does is checks if the &quot;formNAME.fieldNAME&quot; has an at-sign (@) and a period (.) in the there...it also throws an error, and at the same time clears the field and puts the foucs on it...
You call this function in the <body onSubmit=&quot;return functionNAME()&quot;>
Its really easy and works in both IE and NS... I have not failed; I merely found 100,000 different ways of not succeding...
 
<!-- In the HEAD -->

<HEAD>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>

FormName = &quot;form&quot; //Name of the form
CheckName = &quot;check&quot; //Name of the checkboxes (without 1,2,3...10,11,12...)
TextName = &quot;text&quot; //Name of the textfields (without 1,2,3...10,11,12...)

var Condition=new Array(
'a!=&quot;&quot;', //Must be filled
'a!=&quot;&quot;', //Must be filled
'a!=&quot;&quot;&&a.length>5&&a.indexOf(&quot;@&quot;)!=-1&&a.indexOf(&quot;.&quot;)!=-1', //Must be longer than 5 characters, have @, and atleast one '.'
'a.length>7&&!(a.split(&quot;/&quot;)[0]*1>12)&&!(a.split(&quot;/&quot;)[1]*1>31)&&!(a.split(&quot;/&quot;)[2]*1<1900)' //Must be longer than 7 characters, first (two) digit(s) NOT greater than 12, second (two) digit(s) NOT greater than 31 and last 4 greater than 1900
);
function docheck(w) {
eval(&quot;a=document.&quot; + FormName + &quot;.&quot; + TextName + w + &quot;.value&quot;);
if (eval(Condition[w])) eval(&quot;document.&quot; + FormName + &quot;.&quot; + CheckName + w + &quot;.checked=true&quot;);
else eval(&quot;document.&quot; + FormName + &quot;.&quot; + CheckName + w + &quot;.checked=false&quot;);
}
// End -->
</script>

</HEAD>

<!-- In the BODY -->

<BODY>

<form name=form>
<input type=checkbox name=check0 onClick=&quot;return false&quot;> First: <input type=text name=text0 onBlur='docheck(&quot;0&quot;)'><BR>
<input type=checkbox name=check1 onClick=&quot;return false&quot;> Last: <input type=text name=text1 onBlur='docheck(&quot;1&quot;)'><BR>
<input type=checkbox name=check2 onClick=&quot;return false&quot;> E-Mail: <input type=text name=text2 onBlur='docheck(&quot;2&quot;)'><BR>
<input type=checkbox name=check3 onClick=&quot;return false&quot;> Birthday (mm/dd/yyyy): <input type=text name=text3 onBlur='docheck(&quot;3&quot;)'><BR>
</form>

 
If you want something small and simple here is an adaptation of a bigger script.

function isValidEmail(emailAddy)
{
return /^[\da-z+.\-_]+\@[\da-z+.\-_]+\.[\da-z+.\-_]+$/i.test(emailAddy)
}

This is taken from the xapi ( form validation script. Usage is as follows :

if (isValidEmail(&quot;you@wherever.com&quot;))
{
alert(&quot;is a valid email addy&quot;)
}

It doesn't check if the domain name is valid it just checks that the format for the email address is valid (which is all you can do in JS).

Hope this helps. Gary Haran
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top