-2

The following code is not working. May I know what is my problem. Thanks in advance.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.js"></script>
<script src="http://cdn.jsdelivr.net/jquery.validation/1.13.1/jquery.validate.min.js"></script>
<script src="http://cdn.jsdelivr.net/jquery.validation/1.13.1/additional-methods.min.js"></script>
        <script>
$(document).ready(function(){    
   $('#part1').validate({
        rules: {
                family_income: {required: true,min: 0}
        },
        submitHandler: function(form){
                form.submit();
        }
   });

});  
</script>
10
  • 2
    Define "Not working". Any errors in the console? Commented Mar 4, 2015 at 9:04
  • you used $.('#part1').validate({ ... }); change to this one $('#part1').validate ({ -------}); Commented Mar 4, 2015 at 9:04
  • normally.. use numeric() function for validate integer.. Commented Mar 4, 2015 at 9:06
  • may want to use digits or number rules Commented Mar 4, 2015 at 9:10
  • 1
    Getting solved doesn't matter when your question still makes no sense to future readers. Commented Mar 4, 2015 at 19:29

1 Answer 1

1

Use digits to have numeric validation and Update your code with this one. You may add more validations as you like.

$(function(){        
    $('#part1').validate({
       rules: {
           family_income: {
              required: true,
              digits: true
           }
       },
       submitHandler: function(form){
         form.submit();
       }
    });    
}); 

Make sure your element id is family_income in this case.

Update: If you are using data annotations to validate your properties then i would recommend to stick with those and do not mix both the approaches. However, The MVC client-validation features are built on top of the jQuery Validation library, and if you prefer, you can use the Validation library directly like you have used in the example and ignore the MVC features.

So using data annotations you can do something like this, very easily and you can get rid of the custom validator rule defined in the question.

[RegularExpression("[0-9]*")]
[StringLength(50, MinimumLength = 0)] // this will hanle your min length.
// 50 is max limit, you can give as much as you like.
public datatype PropertyName { get; set; }

Note: if your type is int then you do not have to add number validation as the validator will intelligently handle this but if your type is something else like it appears in your case then its worth putting the annotation here.

Alternative: you can also make your text box to allow only numerical input. see this link.

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

4 Comments

The problem is the same code is running ok is a page where the in case of un validation text messages are appearing below the input boxes. But in the page where the code is not working in case of un validation , the input boxes are getting high lighted with red borders. Any idea
This could be because you might have decorated your properties with dataannotations and they inject custom data-attributes understandable by your validator hence putting an error class (input-validation-error) on elements. Can you update this thread with some code from modal also?
All I have done that excluding "family_income" textbox, all other input elements are created with "required" mentioned in the <input> tags. They are all taking text as input. But as this "family_income" textbox needs a numeric validation check, I used the above code. May be that is the problem here.
Mr. Rohit416@ Thank you for your reply. " Mixing both the approaches" was the problem. You are right. Thanks very much. Your codes worked after removing data annotations

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.