-1
<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>
Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73
S.K.
  • 35
  • 1
  • 8
  • why is $("#mobile").val(); in quotes...? – Sudhir Bastakoti Jul 03 '14 at 05:31
  • remove quotes from var merror = '$("#mobile").val()'; – Bhushan Kawadkar Jul 03 '14 at 05:31
  • It would have been more helpful for you to have explained that the "custom method" you're talking about is for the jQuery Validate plugin. Please explain in detail, exactly what "not working" means. Also, edit your OP to include the relevant HTML markup. – Sparky Jul 03 '14 at 05:39
  • And again, where is the relevant HTML? – Sparky Jul 03 '14 at 05:49
  • @Sparky , Sir , but this should need only jQuery Validation Code.Code to do validate Should be sufficient like op has given. – Pratik Joshi Jul 03 '14 at 05:51
  • @PratikJoshi, Sir, please familiarize yourself with this: http://stackoverflow.com/help/mcve ~ Not only does it help us understand where the OP is going wrong, it makes it easier to supply a working demo with the answer. – Sparky Jul 03 '14 at 05:54
  • @pratik still error msg not showing on browser – S.K. Jul 03 '14 at 06:03
  • What kind of error message ? I mean for digits between 0-9? – Pratik Joshi Jul 03 '14 at 06:05
  • What does being new here have to do with writing out what you want? Do you understand the core concept? You are expected to **explain** to us, with **detail and specificity**, _what you're trying to do and how it's failing_. Simply dumping chunks of code and expecting us to know what you want is unacceptable. **Start by reading this please**: http://stackoverflow.com/help/how-to-ask – Sparky Jul 03 '14 at 06:22
  • The title contains the only shred of explanation, _"custom method not working"_, which is a duplicate. If you have something more specific to ask, please post a new question, but first, **please take the time to follow these guidelines**: http://stackoverflow.com/help/how-to-ask – Sparky Jul 03 '14 at 06:30

1 Answers1

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
  • @Sparky , He wants ready-made method for His logic – Pratik Joshi Jul 03 '14 at 06:05
  • @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