I was wondering how to make it so that I could make a rule where a field is not equal to a value. Like I have a field called 'name' so I don't want 'name' = 'Your Name.'
Does anybody have an idea of how to do this? thanks for any help.
I was wondering how to make it so that I could make a rule where a field is not equal to a value. Like I have a field called 'name' so I don't want 'name' = 'Your Name.'
Does anybody have an idea of how to do this? thanks for any help.
You could use a custom method, something like this:
jQuery.validator.addMethod("notEqual", function(value, element, param) {
return this.optional(element) || value != param;
}, "Please specify a different (non-default) value");
Then use it like this:
$("form").validate({
rules: {
nameField: { notEqual: "Your Name" }
}
});
Adding it as a rule like this makes it more extensible, so you can use it to compare against the default value in other fields.
// this one requires the value to be not the same as the first parameter
$.validator.methods.NotEqual = function(value, element, param) {
return value != param;
};
$('form').validate({
rules: {
name: {
NotEqual : 'Your Name'
}
}
});
you can view something similar here.
If you have just one value where you want it to not be like your question implies, you can check against that pretty easily without any external plugins.
$('#yourForm').submit(function(e) {
if ( $('input[name="yourField"]').val()=='Your Name' )
e.preventDefault();
alert("Your message here");
}
});
Not sure if you got your answer but:
return this.optional(element) || value != param;
won't work if the value is variable.
It needs to read:
return this.optional(element) || value != $(param).val();
Cheers