0

Here is my jquery code for form validation: I am facing issue with below code like even replace the expression with the field the functionality is not working

below is the code:-

$(document).ready(function() {
  $(".submit").click(function() {
    $("#myForm").validate({
      rules: {

        'name': {    // this is value of the name attribute of your field
          required: true,
        },

       'email':{
           required:true,
          // regex: 'regex for email field goes here'
        } ,
      'phone':{
        required: true,
      }
      },
      messages: {
        'name': {
          required: "Name is required"
        },

        'email': {
          required: "Email is required",
          //regex: 'Error response for regex fail goes here'
        },
         'phone':{
        required: "phone number is required",
      }
  }

    });


  });

});

Now how can i add the regex expressions for email phone number,name field.

i am new to jquery can anyone help me adding the expression?

Bhairav
  • 87
  • 12
  • Possible duplicate of [Regex not working in jquery validation plugin](http://stackoverflow.com/questions/20946751/regex-not-working-in-jquery-validation-plugin) – J Santosh Oct 12 '15 at 11:19

2 Answers2

1

From add regex to jquery.validate,

$.validator.addMethod("regx", function(value, element, regexpr) {          
    return regexpr.test(value);
}, "Enter an valid email.");
$("#myForm").validate({
    rules: {
        'email':{
            required:true,
            regx: /[^\s@]+@[^\s@]+\.[^\s@]+/
        }
    },
    // ...
});
Community
  • 1
  • 1
William Chan
  • 277
  • 2
  • 12
0

For Example,

$("#myForm").validate({
   rules: {
       experience: {
           required: true,
           regex: /^[0-9]+$/
       }
   }
 });

Working Demo

Insane Skull
  • 9,220
  • 9
  • 44
  • 63