I was using the JQuery Validation plugin, but unfortunately it does not work well with another plugin I am using. Therefore, I am now writing my own custom validation. At the moment I have something like this
function doValidation(formData, jqForm, options) {
var fullName = $.trim($("#fullName").val());
var emailAdd = $.trim($("#emailAdd").val());
var aNum = $.trim($("#aNum").val());
if(fullName.length == 0) {
alert("Please enter your full name");
return false;
}
if(fullName.length > 50) {
alert("Name is to long");
return false;
}
if( !validateEmail(emailAdd)) {
alert("Invalid email");
return false;
}
}
function validateEmail($email) {
return /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test($email);
}
Now obviously I don't want to alert the errors, this is for testing. I also don't want to return false on all of them.
What I am looking to do is add all the errors to an array or something, because if validation fails I need to display all of the errors. I also need to return false if validation fails so my form does not submit. Each input may have more than one validation rule, but only one will be triggered at any time.
How would I go about doing this? I am looking to append all errors to a div with an id validation-results.
Any advice appreciated.
Thanks