I'm using the following code (with jQuery Validation Plugin) to validate an email address:
$(".schedule-tour-form").validate({
rules: {
Email: {
required: true,
email: true
}
},
});
<input id="email" name="Email" class="email" type="email" placeholder="[email protected]" />
Problem is it still allows emails without domain suffixes. For example it validates "test@test" How to I require a domain suffix?
The official page of plugin, even consider test@test
as a valid mail, then I suppose that this is intended. You could create a new rules, with a strict regex pattern, as suggested here.
//custom validation rule
$.validator.addMethod("customemail",
function(value, element) {
return /^([a-zA-Z0-9_.-+])+\@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/.test(value);
},
"Sorry, I've enabled very strict email validation"
);
Then to your rules add:
rules: {
email: {
required: {
depends:function(){
$(this).val($.trim($(this).val()));
return true;
}
},
customemail: true
},
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With