I am currently using JQuery Validate plugin in along with some tip plugin to validate a form. I need to validate the e-mail address as well, other then just checking all fields have been filled out. I am familiar with the Validate rules function, however it causing a problem.
As soon as the e-mail evaluates to be invalid, with my current implementation, all following text fields appear to be invalid (by generating a plugin tip) and causes all following fields to have an annoying tip saying this field is required before the user even had a chance to fill them out.
I was wondering if there was a way to check if my e-mail validation function returned true from within the validate call, where if it does not return true, then consider the form invalid.
Later on I will need to implement a password regex function that will follow the same logic.
My Form Validation
$(".form").validate({
errorPlacement: function(error, element) {
},
highlight: function(element, errorClass, validClass) {
$(element).css('border', '1px solid red');
$('.addlefttip').qtip({
position: {
my: 'middle left',
at: 'middle right'
},
show: {
event: 'focus'
},
hide:{
event: 'blur'
},
style: {
classes: 'qtip-blue qtip-shadow'
},
content: {
attr: 'alt'
},
});
$('.addtoptip').qtip({
position: {
my: 'bottom left',
at: 'top right'
},
show: {
event: 'focus'
},
hide:{
event: 'blur'
},
style: {
classes: 'qtip-blue qtip-shadow'
},
content: {
attr: 'alt'
},
});
},
rules: {
email: {
minlength: 5
}
},
unhighlight: function(element, errorClass, validClass) {
$(element).css('border', '1px solid #ddd');
}
});
My e-mail validation:
$('.email-field').change(function(){
var email = $(this).val();
var re = /^(([^<>()[\]\\.,;:\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,}))$/;
if(re.test(email)) {
document.getElementById('result').innerHTML = '<span style="color:green; font-style:italic; font-weight:normal;"><img src="http://myhousesforsaleindetroit.com/wp-content/uploads/2012/08/green-check-mark.png"> valid';
} else {
document.getElementById('result').innerHTML = '<img src="http://upload.wikimedia.org/wikipedia/en/archive/8/89/20070603232424!RedX.png" style="width:15px; margin:-2px 0px 0px 0px;"/> invalid';
}
});
I appreciate any suggestions!
Many thanks in advance!