I have an HTML form that contains an input which requests a users email address. There are several email addresses that if entered should not allow the form to submit e.g. @donotsubmit.com, @donot.submit.com.
Using jQuery, is it possible to prevent the form from submitting if the input contains these strings? The code looks like this:
<input class="form-control emailrequired" id="Email" name="Email" placeholder="Email" type="text" value="" />
<script type="text/javascript">
$().ready(function () {
$.validator.addMethod("cRequired", $.validator.methods.required, "Please ensure this field is complete");
$.validator.addClassRules("emailrequired", { cRequired: true, email: true });
});
</script>
Edit:
Updated script using jQuery Array. The issue I am having is that the jQuery Validate is not using the inArray statement to mark the field as valid/invalid.
<input class="form-control emailrequired" id="Email" name="Email" placeholder="Email" type="text" value="" />
<script type="text/javascript">
$().ready(function() {
$.validator.addMethod("cRequired", $.validator.methods.required, "Please ensure this field is complete");
$.validator.addMethod("domain", function(value, element) {
var emailAddress = $('#Email').val();
var emailDomain = emailAddress.substr(emailAddress.search('@') + 1);
var invalidAddresses = ["donotsubmit.co.uk"];
if (jQuery.inArray(emailDomain, invalidAddresses) !== -1) {
event.preventDefault();
}
}, "Please use a personal email account");
$.validator.addClassRules("emailrequired", {cRequired: true, email: true, domain: true});
});
</script>