How to Add a Custom Function in Jquery Form Validator

How to Add a Custom Function in Jquery Form Validator?

 

First, write the custom function like below.

jQuery.validator.addMethod("mobile_prefix", function(value, element) 
{

        var select=true;

        var mobile_prefix=$("#mobile_prefix").val();

        if(value && !mobile_prefix)
        {
            select=false;
        }

        return select;

    });

 

Secondly, In the form validator function call the above custom defined function. It will return TRUE / FALSE.

 

 $("#form").validate({

        rules:{
            "password":{
                required:true,
                noSpace: true
            },
            "confpassword": {
                equalTo:"#passwordd"
            },
            "mobile_number":{
                required:true,
                requiredcustom:true,
                numericvalue:true,
                mobile_prefix:true
            },
          },  

          messages: {
            "confpassword": {
                required:"Enter confirm password",
                equalTo: "Password mismatch",
            },
            "password":{
                required:"Enter password",
                noSpace : "no space please."
            },
            "mobile_number": {
                required: "Enter Mobile Number",
                minlength: "Mobile number atleast 10",
               mobile_prefix:"Please select code"
            },
        },
    });

If you like the post, please comment below & follow us on social platform.

Jquery- on Form Submit Validate

On Form Submit Validate using Jquery

<script type="text/javascript"> 
$(document).ready(function()
{ 
    $("#shr_form").on('submit', function()
    { 
       var dForm = $(this); 
       var damount  = dForm.find("input[name=damount]").val(); 
       var dMsgElmnt= dForm.find("#dmessage");
 
       dMsgElmnt.html(''); 
       dMsgElmnt.css('color',''); 

       if(damount=="") { 
          dMsgElmnt.css('color','red');
          dMsgElmnt.html("Please Enter a Minimum Amount."); 
          return false; 
       }else{ 
          var r = confirm("Do you want to submit?"); 
          if (r == true) 
          {     
             return true; 
          } else {    
              return false; 
          } 
       } 
    }) 
});
</script>