-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>
Sparky
  • 98,165
  • 25
  • 199
  • 285
Gablu Nag
  • 3
  • 6
  • 2
    Define "Not working". Any errors in the console? – Jeremy Thille Mar 04 '15 at 09:04
  • you used $.('#part1').validate({ ... }); change to this one $('#part1').validate ({ -------}); – rish Mar 04 '15 at 09:04
  • normally.. use numeric() function for validate integer.. – rish Mar 04 '15 at 09:06
  • may want to use [digits](http://jqueryvalidation.org/digits-method/) or [number](http://jqueryvalidation.org/number-method/) rules – Arun P Johny Mar 04 '15 at 09:10
  • Sir, I have $('#part1').validate ({ -------}); also. But not working. – Gablu Nag Mar 04 '15 at 09:11
  • **No idea what you're really asking** and we cannot see the HTML markup for your form. – Sparky Mar 04 '15 at 19:02
  • Problem solved!!!! See Rohit416 's answer and my comments below. – Gablu Nag Mar 04 '15 at 19:17
  • 1
    Getting solved doesn't matter when your question still makes no sense to future readers. – Sparky Mar 04 '15 at 19:29
  • Sir, I have given hints in my comment above that the matters needed make any sense is given below. At first, when I posted the above question it appeared to me that the issue is related to JQuery script only. Later as per Mr. Rohit's answer I understood that the problem was elsewhere. Fortunately, Mr. Rohit by his knowledge find the "sense" out of my non-sense question. Otherwise I would not be able to solve the problem. – Gablu Nag Mar 04 '15 at 20:11
  • Nobody wants to figure out "hints" given in comments and you missed my point entirely... SO is about ***helping future readers** more than helping yourself*. If you want to make a meaningful contribution, please fix your question so that it makes sense to ***others seeking help***, which would include **adding the relevant HTML** and clearly explaining what you want it to do. – Sparky Mar 05 '15 at 18:18

1 Answers1

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.

Community
  • 1
  • 1
Rohit416
  • 3,416
  • 3
  • 24
  • 41
  • 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 – Gablu Nag Mar 04 '15 at 09:32
  • 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? – Rohit416 Mar 04 '15 at 10:06
  • All I have done that excluding "family_income" textbox, all other input elements are created with "required" mentioned in the 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. – Gablu Nag Mar 04 '15 at 10:26
  • 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 – Gablu Nag Mar 04 '15 at 18:37