I am fairly new to jQuery validation http://validation.bassistance.de/ and am trying to create a custom event using addMethod. I have coded what appears to look correct but obviously isn't because if the event is true, then #email should be hidden with no message.
What is happening is that if the event is true or false, the message 'Please enter a value for testing!' is always fired. I have obviously made an error of epic proportions but cannot seem to find a way to correct it. I would be grateful if someone could point out my error. I have only posted the relevant piece of code. Thanks
Fiddle code: http://jsfiddle.net/DQjTc/
UPDATE: Just an update. I have noticed that if the submit button is pushed after validation, the correct message is entered and the false message does not. Which is what I need. Where do we go from here.
addMethod code
$(function() {
$.validator.addMethod( 'emailHide', function(value, element)
{
if (element.value === '')
{
return false;
}
//return true;
$('#email').hide();
$('#emaillbl').html('Email address approved');
},'Please enter a value for testing!'
);
});
validation rules code
$(function () {
$.validator.setDefaults(
{
errorClass: 'form_error_frmreport',
errorElement: 'div'
});
$("#frmreport").validate(
{
rules:
{
email:
{
//required: true,
email: true,
emailHide: true
},
position:
{
required: true
},
feedback:
{
required: true
}
},
messages:
{
email:
{
//required: "<br />* required: You must enter a valid email address"
},
position:
{
required: "<br />* required: Please state your position"
},
feedback:
{
required: "<br />* required: Please enter as much information regarding the exact nature of the problem"
}
},
submitHandler: function() {
if ($("#frmreport").valid() === true) {
var data = $("#frmreport").serialize();
$.post('/sample/admin/frm10010.php', data, function(msg) {
var messageOutput = '';
for (var i = 0; i<msg.length; i++){
messageOutput += msg[i].box+' ';
}
$("#confirm_department").hide();
var $dialog = $('<div id="dialog"></div>')
.html('Your report was successfully submitted and a representative will respond to you shortly.<br /><br />Thank you.');
$dialog.dialog({
autoOpen: true,
modal: true,
title: 'Report submission successfull',
width: 400,
height: 200,
draggable: false,
resizable: false,
buttons: {
Close: function() {
$( this ).dialog( "close" );
}
}
});
$("#frmreport").get(0).reset();
}, 'json');
} else
{
return;
}
},
success: function() {
//validator.resetForm();
//$.html("You have entered a box");
//$("#BA_boxform").get(0).reset();
}
});
});