/    Sign up×
Community /Pin to ProfileBookmark

Little help with this e-mail validation function

It works when I don’t input a correct e-mail format, but when I do, it won’t let me go to the next field, it focuses on that field and selects it. What am i missing?

Thanks!

function checkEmail(field, fieldname){
var x = field.value;
var filter = /^([a-zA-Z0-9_.])+@(([a-zA-Z0-9])+.)+([a-zA-Z0-9]{2,4})+$/;
if(field.value.length > 0 ) {
if (filter.test(x));
else alert(‘Oops! This doesn’t appear to be a valid e-mail address.’);
field.focus();
field.select();
}
}

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@LogicianMay 25.2006 — It works when I don't input a correct e-mail format, but when I do, it won't let me go to the next field, it focuses on that field and selects it. What am i missing?

Thanks!

function checkEmail(field, fieldname){

var x = field.value;

var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;

if(field.value.length > 0 ) {

if (filter.test(x));

else alert('Oops! This doesn't appear to be a valid e-mail address.');

field.focus();

field.select();

}

}[/QUOTE]


field.focus(); & field.select(); are being called regardless of the result.
Copy linkTweet thisAlerts:
@biniciauthorMay 25.2006 — field.focus(); & field.select(); are being called regardless of the result.[/QUOTE]

So, I need to return a false somewhere in there? I just want to call the focus and select, when it is incorrect? But I am having a difficult time. Could you advise me please?
Copy linkTweet thisAlerts:
@LogicianMay 25.2006 — So, I need to return a false somewhere in there? I just want to call the focus and select, when it is incorrect? But I am having a difficult time. Could you advise me please?[/QUOTE]

[URL=http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Statements:if...else]When all else fails, read the f[/URL]
Copy linkTweet thisAlerts:
@biniciauthorMay 25.2006 — [URL=http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Statements:if...else]When all else fails, read the f[/URL][/QUOTE]

Argh me so confused! ? Could you please help me on this one? Thanks!
Copy linkTweet thisAlerts:
@felgallMay 25.2006 — Replace:

if (filter.test(x));
else alert('Oops! This doesn't appear to be a valid e-mail address.');
field.focus();
field.select();


with:

if (!filter.test(x)) {
alert('Oops! This doesn't appear to be a valid e-mail address.');
field.focus();
field.select();
}
Copy linkTweet thisAlerts:
@biniciauthorMay 25.2006 — Replace:

if (filter.test(x));
else alert('Oops! This doesn't appear to be a valid e-mail address.');
field.focus();
field.select();


with:

if (!filter.test(x)) {
alert('Oops! This doesn't appear to be a valid e-mail address.');
field.focus();
field.select();
}
[/QUOTE]


Thanks! Wow silly overlooks like that can kill you when trying to figure out javascript logic. Thanks works great!
×

Success!

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