/    Sign up×
Community /Pin to ProfileBookmark

Email Validation

I have a script that goes something like …

[CODE]var input = document.newsletter.email;

if (/^([_a-z0-9-]+)(.[_a-z0-9-]+)*@([a-z0-9-]+)(.[a-z0-9-]+)*(.[a-z]{2,4})$/.test(input.value)) {
// email is valid so send it code goes here
}[/CODE]

This code is more elaborate than what I’ve written and works fine … but that’s not why I’m here.

I’m here because I’m trying to configure a totally different <div> element to say something like …

[CODE]var myDiv = document.getElementById(‘someDiv’);

myDiv.onmouseup = new Function(“if (!/^([_a-z0-9-]+)(.[_a-z0-9-]+)*@([a-z0-9-]+)(.[a-z0-9-]+)*(.[a-z]{2,4})$/.test(input.value)) {input.value = input.defaultValue;”);[/CODE]

Note the exclamation point in my second example. Basically I’m trying to say “if the value of the input is NOT a valid email address, reset the input value”.

For some reason, my email checksum works on form submission but not in regards to my second example.

ie – if I type “asdf@” and hit submit, invalid. If I type “asdf@asdf” and hit submit, invalid. If I typed “[email protected]” and hit submit, valid – BUT – if I mouseup on a the div in question and the input value is “anything@” (note the @ sign with nothing after it which would be invalid), it thinks it’s valid.

Why? How do I fix this?

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @robindean 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.18,
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,
)...