/    Sign up×
Community /Pin to ProfileBookmark

Email Validation!

[URL=”http://www.outlook-group.com/outlook_images/v3/information/contact_us.php”]http://www.outlook-group.com/outlook_images/v3/information/contact_us.php[/URL]

Hello all,

I’m trying to do some simple email validation. I have the following javascript:

[CODE]// Email Validation Javascript
function validate_email(addr,man,db)
{
if (addr == ” && man) {
if (db) alert(’email address is mandatory’);
return false;
}
if (addr == ”) return true;
var invalidChars = ‘/’\ “;:?!()[]{}^|’;
for (i=0; i<invalidChars.length; i++) {
if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
if (db) alert(’email address contains invalid characters’);
return false;
}
}
for (i=0; i<addr.length; i++) {
if (addr.charCodeAt(i)>127) {
if (db) alert(“email address contains non ascii characters.”);
return false;
}
}

var atPos = addr.indexOf(‘@’,0);
if (atPos == -1) {
if (db) alert(’email address must contain an @’);
return false;
}
if (atPos == 0) {
if (db) alert(’email address must not start with @’);
return false;
}
if (addr.indexOf(‘@’, atPos + 1) > – 1) {
if (db) alert(’email address must contain only one @’);
return false;
}
if (addr.indexOf(‘.’, atPos) == -1) {
if (db) alert(’email address must contain a period in the domain name’);
return false;
}
if (addr.indexOf(‘@.’,0) != -1) {
if (db) alert(‘period must not immediately follow @ in email address’);
return false;
}
if (addr.indexOf(‘.@’,0) != -1){
if (db) alert(‘period must not immediately precede @ in email address’);
return false;
}
if (addr.indexOf(‘..’,0) != -1) {
if (db) alert(‘two periods must not be adjacent in email address’);
return false;
}
var suffix = addr.substring(addr.lastIndexOf(‘.’)+1);
if (suffix.length != 2 && suffix != ‘com’ && suffix != ‘net’ && suffix != ‘org’ && suffix != ‘edu’ && suffix != ‘int’ && suffix != ‘mil’ && suffix != ‘gov’ & suffix != ‘arpa’ && suffix != ‘biz’ && suffix != ‘aero’ && suffix != ‘name’ && suffix != ‘coop’ && suffix != ‘info’ && suffix != ‘pro’ && suffix != ‘museum’) {
if (db) alert(‘invalid primary domain in email address’);
return false;
}
return true;
}[/CODE]

and the following form:

[CODE]<form action=”contact_us_summary.php” method=”post” name=”email” onsubmit=”return validate_email(document.getElementById(’email_address’).value, 1, 1);”>[/CODE]

When the form is submitted I get the correct error message, however the form is still submitted!

If someone could help me spot my mistake I would be most grateful!

Many Thanks,

dai.hop

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERJun 18.2007 — You are missing a '&' in your primary domain check.

Should be:
[code=php]
if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org'
&& suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov'
&& suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name'
&& suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
[/code]
Copy linkTweet thisAlerts:
@JMRKERJun 18.2007 — Not a change to function, but slight modification for readibility and reduced size and load time. ?
[code=php]
<html>
<head>
<title>Email Validator</title>

<script type="text/javascript">
var EmailError = new Array();
EmailError[0] = 'Valid email format';
EmailError[1] = 'Email address is mandatory';
EmailError[2] = 'Email address contains invalid characters';
EmailError[3] = 'Email address contains non ascii characters.';
EmailError[4] = 'Email address must contain an @';
EmailError[5] = 'Email address must not start with @';
EmailError[6] = 'Email address must contain only one @';
EmailError[7] = 'Email address must contain a period in the domain name';
EmailError[8] = 'Period must not immediately follow @ in email address';
EmailError[9] = 'Period must not immediately precede @ in email address';
EmailError[10] = 'Two periods must not be adjacent in email address';
EmailError[11] = 'Invalid primary domain in email address';

// Email Validation Javascript
function validate_email(addr,man,db) {
var Err = 0;

if ((addr == '') && (man == 1)) { Err = 1; }
if (addr == '') { alert('Missing email address'); return false; }

var invalidChars = '/'\ ";:?!()[]{}^|';
for (i=0; i<invalidChars.length; i++) {
if (addr.indexOf(invalidChars.charAt(i),0) > -1) { Err = 2; }
}
for (i=0; i<addr.length; i++) {
if (addr.charCodeAt(i)>127) { Err = 3;}
}

var atPos = addr.indexOf('@',0);
if (atPos == -1) { Err = 4; }
if (atPos == 0) { Err = 5; }
if (addr.indexOf('@', atPos + 1) > - 1) { Err = 6; }
if (addr.indexOf('.', atPos) == -1) { Err = 7; }
if (addr.indexOf('@.',0) != -1) { Err = 8; }
if (addr.indexOf('.@',0) != -1){ Err = 9; }
if (addr.indexOf('..',0) != -1) { Err = 10; }

var suffix = addr.substring(addr.lastIndexOf('.')+1);
if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org'
&& suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov'
&& suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name'
&& suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') { Err = 11; }

if (Err == 0) {
return true;
} else {
if (db) { alert(EmailError[Err]); }
return false;
}
}

// following needed only for testing
function SetEmail(act) {
if (act == 0) { document.getElementById('email_address').value='[email protected]'; } // valid entry
if (act == 1) { document.getElementById('email_address').value=''; } // blank entry
if (act == 2) { document.getElementById('email_address').value='/'\ ";:?!()[]{}^|@domain.com'; } // invalid chars
if (act == 3) { document.getElementById('email_address').value='UserNamedomain.com'; } // missing '@'
if (act == 4) { document.getElementById('email_address').value='@domain.com'; } // starting '@'
if (act == 5) { document.getElementById('email_address').value='User@[email protected]'; } // dual '@' entries
if (act == 6) { document.getElementById('email_address').value='[email protected]'; } // '.' not allowed after '@'
if (act == 7) { document.getElementById('email_address').value='[email protected]'; } // '.' not allowed before '@'
if (act == 8) { document.getElementById('email_address').value='[email protected]'; } // '.' not allowed twice
if (act == 9) { document.getElementById('email_address').value='[email protected]'; } // invalid ending
if (act == 10) { document.getElementById('email_address').value='UserName@domain.'; } // missing ending
}
</script>
</head>
<body>
<!--
<form action="contact_us_summary.php" method="post" name="email"
onsubmit="return validate_email(document.getElementById('email_address').value, 1, 1);">
-->

<!-- start test section -->
<input type="text" id="email_address" value="" size="40">
<button onClick="alert(validate_email(document.getElementById('email_address').value, 0, 1))">
Validate</button>
<br />
<button onClick="SetEmail(0)">Test 0: Valid</button>
<button onClick="SetEmail(1)">Test 1: blank</button>
<button onClick="SetEmail(2)">Test 2: Invalid chars</button><br />
<button onClick="SetEmail(3)">Test 3: Missing @</button>
<button onClick="SetEmail(4)">Test 4: Starting @</button>
<button onClick="SetEmail(5)">Test 5: Dual @@</button><br />
<button onClick="SetEmail(6)">Test 6: @. not allowed</button>
<button onClick="SetEmail(7)">Test 7: .@ not allowed</button>
<button onClick="SetEmail(8)">Test 8: .. not allowed</button><br />
<button onClick="SetEmail(9)">Test 9: .xxx not allowed</button>
<button onClick="SetEmail(10)">Test 10: blank ending not allowed</button>
<!-- end test section -->
</body>
</html>
[/code]

May want to change back for blank entry if email check is NOT mandatory.

Your choice.

Good Luck!
×

Success!

Help @dai_hop spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...