<script>
$("#form").validate();
$(document).ready(function ()
{
var merror = '';
jQuery.validator.addMethod('mvalid', function(value, element) {
if($("#mobile").hasClass("required") == true && ($("#mobile").val().substr(0,1) != '7' ||$("#mobile").val().substr(0,1) != '8' ||
$("#mobile").val().substr(0,1) != '9')) {
merror = 'Enter correct Mobile No.';
return false;
}
return true;
}, merror);
});
</script>
Asked
Active
Viewed 2,362 times
-1

Pratik Joshi
- 11,485
- 7
- 41
- 73

S.K.
- 35
- 1
- 8
1 Answers
1
You don't need to create a custom method to simply show a custom message for the required
rule. Just use the messages
option.
$('#myform').validate({
rules: {
mobile: {
required: true
}
},
messages: {
mobile: {
required: 'Enter Mobile No.'
}
}
});
DEMO: http://jsfiddle.net/dLq5t/
EDIT:
Otherwise, if you need a custom rule, the message is the last item within. It will display when the element fails validation.
jQuery.validator.addMethod('mvalid', function(value, element, params) {
// your custom function
// return true to pass validation
// return false to fail validation and display error message
}, 'Enter Mobile No.');
See documentation: http://jqueryvalidation.org/jQuery.validator.addMethod/
BTW: You do not need to check for an empty field within your custom function because the required
rule already takes care of that. And you don't need to check for a required
class
, again, because the plugin already does that by default.

Sparky
- 98,165
- 25
- 199
- 285
-
Then use custom method ,and use it as => customMobileValidatin: true below required:true – Pratik Joshi Jul 03 '14 at 06:01
-
@user3682603, see my edit for where to place your custom message within the custom method. – Sparky Jul 03 '14 at 06:03
-
-
@PratikJoshi, if he can make a more specific question, then he'll get a more specific answer. As you can see, the "title" is the only place where he's actually said anything. It's not our job to piece this all together based on comments and mind-reading. – Sparky Jul 03 '14 at 06:07
– S.K. Jul 03 '14 at 05:56