jQuery Validate plugin - don't evaluate empty fields -
i'm using jquery validate plugin , i'm trying should relatively easy, can't work.
i've got input
field not mandatory/required field. if nothing entered field don't want run form of validation on field. if form field changes , contains input want validate against, example, numbers only, if field blank, remove / don't validate.
i tried doing 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 put in code i'm getting error:
uncaught typeerror: object #voucher has no method 'rules'
even though i'm sure validate()
object has been initiated, valform()
function calls validation object , setup default rules etc.
can offer words of wisdom please?
quote op:
i've got input field not mandatory/required field. if nothing entered field don't want run form of validation on field. if form field changes , contains input want validate against, example, numbers only, if field blank, remove / don't validate.
you've described expected normal behavior of form validation software, including jquery validate plugin.
you don't need custom blur
event handler because plugin performs validation triggered onfocusout
option enabled default.
you don't need add & remove rules using rules()
method because plugin, default, not evaluate rules on empty field. (of course, single exception required
rule, definition, applies empty field.)
delete code , start on more this...
html:
<form id="myform"> <input type="text" name="voucher" /> <input type="submit" /> </form>
jquery:
$(document).ready(function() { $('#myform').validate({ rules: { voucher: { number: true } } }); });
working demo: http://jsfiddle.net/hbfjb/
as can see in demo, leaving field blank allows submission, however, when characters entered, validation rule evaluated.
Comments
Post a Comment