0

Very simple requirement, validate the field whether a positive number or not.

please refer to the js fiddle

http://jsfiddle.net/DJMmm/5/

HTML CODE

<form id="new_leavetype_form" action = "<%=path %>/admin/create.go">

     <input name="default_days" id="default_days_id" type="text"  />      
     <button id="submit_button" type="button">Submit</button>

</form>

JS CODE

$().ready(function(){
    $("#new_leavetype_form").validate({
        rules:{
            default_days: { number: true, required:true, PositiveNumber:true }
        }
    });

    $("#submit_button").click(function(){
        $("#new_leavetype_form").submit();
    });

})

Question

if fill letter(s) like 'abc' into the filed, while the filed lost focus, the validation was fired properly, however if the input is a number will cause the exception: 'Cannot call method 'call' of undefined '

I really don't know what caused this exception, please help, thank you.

11
  • You haven't defined the PositiveNumber validation method. Commented Oct 11, 2013 at 17:10
  • there is no rule saying PositiveNumber Commented Oct 11, 2013 at 17:11
  • isn't it a default method? Commented Oct 11, 2013 at 17:12
  • No. The default methods are listed here: jqueryvalidation.org/category/methods Commented Oct 11, 2013 at 17:12
  • 1
    BTW- you can get rid of your entire .click() handler function by simply changing your button to type="submit". See: jsfiddle.net/3tuzK Commented Oct 11, 2013 at 17:15

1 Answer 1

2

There is no validation rule with name PositiveNumber.

Either you need to create a new rule or you can set the min rule to achieve the dame

$("#new_leavetype_form").validate({
    rules: {
        default_days: {
            number: true,
            required: true,
            min: 0
        }
    }
});

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

jquery validate needs to state the missing rule name in the error message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.