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.