I'm having a little trouble getting the following bit of code to work correcty in my script:
if (email == "") {
$("#field2 .error1").show();
$("input#email").focus();
return false;
}
else if(email != /^[a-zA-Z0-9._-]+@[a-zA-Z0-9][a-zA-Z0-9.-]*[\.]{1}[a-zA-Z]{2,4}$/) {
$("#field2 .error2").show();
$("input#email").focus();
return false;
}
Basically I have a form on my page and it's validating a couple of fields using a external js file. I want it to check if the email field is blank or if it doesn't match the regular expression. If it's blank it returns .error and if it's not matching the regular expression I want it to show .error2.
What's happening is it's showing the errors but even if I put in a correct email address it won't validate and just continues showing .error2. I'm pretty stumped what I'm missing here so any help would be appreciated.
Even if I try this,
if (email == "" || email != /^[a-zA-Z0-9._-]+@[a-zA-Z0-9][a-zA-Z0-9.-]*[\.]{1}[a-zA-Z]{2,4}$/) {
$("#field2 .error1").show();
$("input#email").focus();
return false;
}
I still can't get it to send the form???
OK So I've tried this and it seems to be working properly. Does it look correct? lso I'll take into consideration the regx string and use a better one, it was more just to get it working as intended.
var email = $("input#email").val();
var regx = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9][a-zA-Z0-9.-]*[\.]{1}[a-zA-Z]{2,4}$/;
if (email == "") {
$("#field2 .error").show();
$("input#email").focus();
return false;
}
else if (!regx.test(email)){
$("#field2 .error2").show();
$("input#email").focus();
return false;
}