/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] form validation, validateEmail function

I am having trouble validating with the email field in the form. I believe the problem is in the function validateEmail. I need the email field in the form to check anything less than a full email address, in the format of [email][email protected][/email]. If I type in dave_brown.@unh and you’ll see what I mean. Also, my code isn’t testing for blank text boxes,. You can leave out any information on the text box, and the validation will still fall through. Any ideas on how to fix this?

[CODE]

function validateEmail(fld) {
// var error=””;
// var email = document.demo.email.value;
//email = email.replace(/^s+|s+$/, ”);
var tfld = trim(fld.value); // value of field with whitespace trimmed off
// var emailFilter = /^[^@]+@[^@.]+.[^@]*ww$/ ;
var emailPattern = /^[_a-zA-Z0-9\-]+(.[_a-zA-Z0-9\-]+)*@[a-zA-Z0-9\-]+(.[a-zA-Z0-9\-]+)*(.[a-z]{2,3})$/;
var illegalChars= /[()<>,;:\”[]]/ ;
var email = “[email protected]”;

/*
if (emailPattern.test(tfld)) {
window.alert(“You entered a valid e-mail address.”);
}
else {
window.alert(“You didn’t enter a valid e-mail address”);
fld.style.background = ‘Yellow’; }
/* else {
fld.style.background = ‘White’;
}
*/

if (fld.value == “”) {
fld.style.background = ‘Yellow’;
error = “You didn’t enter an email address.n”;

} else if (emailPattern.test(tfld)) {
fld.style.background = ‘Yellow’;
error = “Name has to be in [email protected]”;

} else if (fld.value.match(illegalChars)) { //test email for illegal characters
fld.style.background = ‘Yellow’;
error = “The email address contains illegal characters.n”;

} else if (!(/^([a-z0-9])([w.-+])+([a-z0-9])@((w)([w-]?)+.)+(com|net|org)$/i.test(email))) {
fld.style.background = ‘Yellow’;
error = “Invalid address, top level domain (has to be .com .net or .org).n”;

} else {
fld.style.background = ‘White’;
}
return error;

}

[/CODE]

my whole code:

[url]http://ceweb.uml.edu/jberg14527/Javascript/formvalidationValidate.html[/url]

can’t find the attached file feature, copy and paste from page source to see how it works.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@FangNov 08.2010 — [CODE]function validateEmail(fld) {
var error=""; // define error
[/CODE]
returns false when incorrect [CODE] } else if ([COLOR="Blue"]![/COLOR]emailPattern.test(tfld)) {

fld.style.background = 'Yellow';
error = "Name has to be in [email protected]";
[/CODE]
Copy linkTweet thisAlerts:
@KingJames603authorNov 21.2010 — I replaced emailFilter with emailPattern in function validateEmail and got it to work like a charm. Thank you so much for the help.

[CODE]

function validateEmail(fld) {
var error="";
var email = document.demo.email.value;
email = email.replace(/^s+|s+$/, '');
var tfld = trim(fld.value); // value of field with whitespace trimmed off
var emailPattern = /^[_a-zA-Z0-9\-]+(.[_a-zA-Z0-9\-]+)*@[a-zA-Z0-9\-]+(.[a-zA-Z0-9\-]+)*(.[a-z]{2,3})$/;

var illegalChars= /[()<>,;:\"[]]/ ;


if (fld.value == "") {
fld.style.background = 'Yellow';
error = "You didn't enter an email address.n";

} else if (!emailPattern.test(tfld)) {
fld.style.background = 'Yellow';
error = "Email name has to be in [email protected] format.n";

} else if (fld.value.match(illegalChars)) { //test email for illegal characters
fld.style.background = 'Yellow';
error = "The email address contains illegal characters.n";

} else if (!(/^([a-z0-9])([w.-+])+([a-z0-9])@((w)([w-]?)+.)+(com|net|org)$/i.test(email))) {
fld.style.background = 'Yellow';
error = "Invalid address, top level domain (has to be .com .net or .org).n";

} else {
fld.style.background = 'White';
}
return error;
}

[/CODE]
×

Success!

Help @KingJames603 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

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