views:

2200

answers:

2

I'm using the jQuery Validation plugin and I've got a textbox with the class digits to force it to be digits only, but not required. When I call validate on the form it works fine, but if I call valid() on the textbox when it's empty, it returns 0, despite no error message showing and required not being set.

Does anyone know why it would be returning this for a seemingly valid input value?

Here is the code:

<input type="text" value="" name="kiloMetresTravelled" id="kiloMetresTravelled" class="digits"/>

and the script

<script type="text/javascript'>
 var isvalid = jQuery('#kiloMetresTravelled').valid(); 
 //isvalid == 0 when kiloMetresTravelled is blank
</script>
+4  A: 

Check this, from the documentation:

Makes "field" required and digits only.

You could do something like this:

var isValid = jQuery('#kiloMetresTravelled').valid() || jQuery('#kiloMetresTravelled').val() == "";
CMS
Doesn't it only say 'makes the element require digits only'. They added a 'required' rule as well
Glenn Slaven
+1  A: 

I think this works:

var e="#whatever";
var isValid = $(e).valid() || $(e).val()== "" && !$(e).hasClass('required');
Will