Click to See Complete Forum and Search --> : Email Address Validation


Arc
05-31-2003, 01:10 PM
Hi, I need a simple Email address validation script. I did a search for it on this site but didnt find anything that worked the way i want it to.


Basically i need to check and make sure the string is more than 8 characters long and also has a '.' and a '@' in it.


thanks alot!:D

Nevermore
05-31-2003, 01:21 PM
Assuming that your email address field has id email, this should work:

function emailval(){
var x = document.getElementById('email')value;
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (filter.test(x)) return true;
else {alert('That email address apears to be invalid. Please reenter it.');
return false;}
}

The function should be called onsubmit.

Nevermore
05-31-2003, 01:22 PM
Oh, I didn't see that you only wanted a very basic script. Well, I think that one is pretty much foolproof. Should make sure an email address is in the correct format, although it doesn't ensure it is real.

Charles
05-31-2003, 03:22 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<form action="">
<script type="text/javascript">
<!--
// This one will check for a vaild email address according the the email rules
String.prototype.isEmailAddress = function () {return this.match(/^[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+$/)}

// This one will check to see if the address is more than eight characters long and containing a '.' and a '@'.
String.prototype.isEmailAddress = function () {return this.length > 8 && this.match(/@.+\./)}
// -->
</script>
<div>
<label>Email Address:<br>
<input type="text" onchange="if (!this.value.isEmailAddress()) {alert ('That would not appear to be a valid email address.'); this.value=''; this.focus()}" ></label>
<br><input type="submit">
</div>
</form>

Arc
05-31-2003, 04:40 PM
Cool Thanks guys!:D