Question

I am new to jQuery and i am using jQuery form validator to validate my form..

The problem is , i have to validate the password as it must contain at-least one digit..

I searched on Google and I found example in here Link to forum.jquery

Here is the snippet of my HTML code

  <form  name="f2" id="f2" method="post" action="hello">
     <table>
      <tr>
             <th>
                 Password
             </th>
              <td>
             <input type="password" name="pwd" id="pwd"  placeholder="Enter Password" class="text_box">
              </td>
      </tr>
      <tr>
           <td>
              <button type="submit" class="submit-button">Register</button>
           </td>
      </tr>

     </table>
    </form>

    //jquery script for form validation

      <script>
        $(document).ready(function(){
            $("#f2").validate({
                debug: false,
                rules: { 

                 pwd:{
                        required:true,
                        minlength:5
                    }

                 },
                messages:
                    {
                      pwd:{
                        required:"Please enter password",
                        minlength:"Please enter minimum characters"
                    }
                 }
             });
        });
    </script>

Everything works fine until i add the this line to above code rules

pwd:{
    required:true,
    minlength:5,
    mypassword: true
}

I included the additional scripting file which contains the following method

jQuery.validator.addMethod('mypassword', function(value, element) 
{
    return this.optional(element) || (value.match(/[a-zA-Z]/) && value.match(/[0-9]/));
},
'Password must contain at least one numeric and one alphabetic character.');

After trying to execute the above code, I got the following error message in my browser console as follows:

Uncaught TypeError: Cannot call method 'call' of undefined in js/jqueryvalidation.js

Why the additional method not working?

Any help or suggestions would be greatly appreciated.

Was it helpful?

Solution

Please check the example in the fiddler link : http://jsfiddle.net/NaaEw/

    jQuery.validator.addMethod('mypassword', function(value, element) 
    {
       return this.optional(element) || (value.match(/[a-zA-Z]/) && value.match(/[0-9]/));
    });

and message :

    pwd:{
                    required:"Please enter password",
                    minlength:"Please enter minimum characters",
      mypassword: 'Password must contain at least one numeric and one alphabetic character.'

Hope this is what you are expecting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top