Reputation: 11
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;
}
Upvotes: 1
Views: 508
Reputation: 964
Here try this Head:
<script type="text/javascript">
function validateEmail(theForm) {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(theForm.email-id.value))
{
return(true);
}
alert("Invalid e-mail address! Please enter again carefully!.");
return(false);
}
</script>
Body:
<form onSubmit="return validateEmail(this);" action="">
E-mail Address:
<input type="text" name="emailid" />
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</form>
Upvotes: 0
Reputation: 170
What you are comparing is a regular expression.
var filter = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9][a-zA-Z0-9.-]*[\.]{1}[a-zA-Z]{2,4}$/;
if (!filter.test($("input#email").val()))
{
// invalid email
}
//valid email
Upvotes: 0
Reputation: 3726
Try this
var regexp=/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i;
if(regexp.test(email)){
// matched
}else{
// not matched
}
Upvotes: 2
Reputation: 21366
you need do do like this,
var regx=/^[a-zA-Z0-9._-]+@[a-zA-Z0-9][a-zA-Z0-9.-]*[\.]{1}[a-zA-Z]{2,4}$/ // create your regular expresseion
if(regx.test(email)){// use test() method to do the match
// do if match
}else{
// not matched
}
Upvotes: 0