I'm using the jQuery Validate plugin and what I'm trying to do should be relatively easy, but I can't get it to work.
I've got an input
field that is not a mandatory/required field. If nothing is entered into the field I don't want to run any form of validation on the field. If the form field changes and contains any input I want to validate it against, for example, numbers only, but if the field is blank, remove / don't validate.
I tried doing something like this:
valForm('excessFrm');
var vou = $('input[name="voucher"]');
vou.blur(function(){
if($(this).val() == ''){
( "#voucher" ).rules( "remove" );
} else {
( "#voucher" ).rules( "add", {
number: true
});
}
})
But no matter where I put this in my code I'm getting this error:
Uncaught TypeError: Object #voucher has no method 'rules'
Even though I'm sure the validate()
object has been initiated, the valForm()
function calls the validation object and setup all my default rules etc.
Can anybody offer some words of wisdom please?