/    Sign up×
Community /Pin to ProfileBookmark

email validation returns invalid due to underscore

Hi. My form validation page calls an email validation script and that email validation in turn invokes the below javascript to ensure that only valid characters are included in the email address.

However, as we all know, underscores are valid.

The below script apparently is saying that underscores are not valid.

I would have guessed that using the “lowercase” function as this script does, would mean that the underscore should work… but it doesn’t. ALso, I tried including an underscore as one of the regexp characters and that also didn’t work.

Do you know how I could change this so that underscores aren’t found to be invalid (they should at least be valid if they occur prior to the @.. I am not sure about after the @). Thanks.

====

function allValidChars(email) {
var parsed = true;
var validchars = “[email protected]“;
for (var i=0; i < email.length; i++) {
var letter = email.charAt(i).toLowerCase();
if (validchars.indexOf(letter) != -1)
continue;
parsed = false;
break;
}
return parsed;
}

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Jeff_MottJan 15.2005 — You might try this regular expression instead.String.prototype.isValidEmail = function() {
return /^[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+(?:.[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+)*@[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+(?:.[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+)+$/i.test(this);
};
Copy linkTweet thisAlerts:
@swoop80authorJan 15.2005 — First, thanks for your help. It's really appreciated.

I was trying to

a) make sense of the expression you provided (I'm really new to this); and

b) figure out how to use it instead of my current email validation...

Do you have a sample of it's usage? I can't figure out how I'd use it in place of what I have now... sorry.

Again - thanks for your help.
Copy linkTweet thisAlerts:
@Jeff_MottJan 16.2005 — The expression I provided is a regular expression. You can read through [url=http://synchro.net/docs/js/ref/regexp.html#1193136]RegExp in the JavaScript Core Reference[/url].

As for how to use it ...String.prototype.isValidEmail = function() {
return /^[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+(?:.[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+)*@[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+(?:.[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+)+$/i.test(this);
};

if( eml_string.isValidEmail() ) {
// email is good
}
else {
// email is bad
}
Copy linkTweet thisAlerts:
@swoop80authorJan 16.2005 — thanks. i'll give it a try.
×

Success!

Help @swoop80 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.22,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

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