I’ve got the jQuery validate script working, and I’ve added (found) a function to prevent anyone from registering a free email (Yahoo, Gmail, etc.) but the validation is not working and is letting any email address be submitted. Any idea?
$(document).ready(function() {
$.validator.addMethod('nofreeemail', function (value) {
return /^([w-.]+@(?!gmail.com)(?!yahoo.com)(?!hotmail.com)([w-]+.)+[w-]{2,4})?$/.test(value);
}, 'Free email addresses are not allowed.');
// validate signup form on keyup and submit
$("#registerform").validate({
rules: {
username: {
required: true,
minlength: 2,
nofreeemail: true
},
password: {
required: true,
minlength: 5
}
},
messages: {
username: {
required: "Please enter a username",
minlength: "Your email must consist of at least 2 characters",
nofreeemail: "Please use your business email, we don't accept Gmail, Yahoo, Hotmail accounts."
},
password: {
required: "Please provide a password",
minlength: "Your password must be at least 5 characters long"
}
}
});
});
Brilliant, thank you fretburner! I have a working test page up here, and one last weird behavior I am seeing now is if you enter an invalid email like “asdf” and move on to the password, its not telling the user to enter a valid email, its saying to enter a non google, yahoo, etc email.
I’d suggest getting rid of the 4 - there are lots of valid top level domains now that have more than four letters. One they have been promoting around here lately is .sydney (six letters after the dot)
No. You now are restricting it to just two characters after the last dot - you need the comma after the 2.
So the validation order is wrong and you are testing for not a free email address first before testing for a valid email address. The regular expression you are using checks for both so if it isn’t a valid email address then it fails the test just as it would if it were a free address.