im trying to validate my form,
but for some reason the SSN field wont work 🙁
i have:
validate: function () {
contact.message = '';
if (!$('#contact-container #contact-name').val()) {
contact.message += 'Name is required. ';
}
var ssn = $('#contact-container #contact-ssn').val();
if (!ssn) {
contact.message += 'SSN is required. ';
}
else {
if (!contact.validateSSN(ssn)) {
contact.message += 'SSN is invalid. ';
}
}
var email = $('#contact-container #contact-email').val();
if (!email) {
contact.message += 'Email is required. ';
}
else {
if (!contact.validateEmail(email)) {
contact.message += 'Email is invalid. ';
}
}
if (!$('#contact-container #contact-message').val()) {
contact.message += 'Message is required.';
}
if (contact.message.length > 0) {
return false;
}
else {
return true;
}
},
and
validateSSN: function (ssn) {
//
if (!/^[0-9]{9}$/)
return false;
else
{
return true;
}
},
The e-mail section i have works fine, but the SSN section just accepts anything along as its not empty. What am i missing?
thank you
!/^[0-9]{9}$/ will always return false since the /…/ part evaluates to a regex object and an object is always evaluated to true.
You probably want to
if (!/^[0-9]{9}$/.test(ssn))EDIT: Even simpler: