Anubhav
Anubhav

Reputation: 1625

Regex not working in jquery validation plugin

I am using below code

$(function() {
    $("#myForm").validate({
        rules: {
            experience: {
                required: true,
                regex: '^[0-9]$'
            }
        },
        messages: {
            experience: {
                required: "Please provide experience",
                regex: "Provide a valid input for experience"
            }
        }
    });
});

But the above code is not taking 2 or 22 as valid input? What I am doing wrong? Help required...

Upvotes: 7

Views: 21589

Answers (4)

AnimaLity
AnimaLity

Reputation: 1

In my case, my email field had it's own regex pattern data like

<input id="email" name="email" data-val-regex-pattern="^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"> 

which conflicted with the pattern I injected for JQuery Validation plugin. I just removed this data- attribute and it worked.

Upvotes: 0

Abhishek
Abhishek

Reputation: 1618

You can use 'pattern' keyword instead of 'regex' in .validate() form of jQuery. Below is sample code :

$('#registerForm').validate({
    rules: {
        "customer[is_seller]": {
            required:true
        },
        "customer[fname]": {
            required:true,
            pattern:/^[A-Za-z]+$/
        },
        "customer[lname]": {
            required:true,
            pattern:/^[A-Za-z\s]+$/
        },
        "customer[email]": {
            required:true,
            email:true
        },
        "customer[password]": {
            required:true
        }
    },
    messages:{
        "customer[is_seller]": {
            required:"Please select Your category"
        },
        "customer[fname]": {
            required:"Please enter first name.",
            pattern:"Special characters not allowed"
        },
        "customer[lname]": {
            required:"Please enter last name."
            pattern:"Special characters not allowed"
        },
        "customer[email]":{
            required:"Email Id is required",
            email:"Please enter a valid email."
        },
        "customer[password]":{
            required:"Password is required"
        }
    }
});

Upvotes: -1

Stephan
Stephan

Reputation: 43013

Try this regex instead:

^[0-9]+$

Then put it in the code:

$(function() {
   $.validator.addMethod("regex", function(value, element, regexpr) {          
     return regexpr.test(value);
   }, "Please enter a valid pasword.");    

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

Here is a working demo:

http://jsfiddle.net/4PuJL/1/

Upvotes: 15

Somnath Kharat
Somnath Kharat

Reputation: 3610

There is no regex method in validate jquery: You have to create of your regex method of your own

You need to use addmethod

$.validator.addMethod("regx", function(value, element, regexpr) {          
    return regexpr.test(value);
}, "Provide a valid input for experience.");

Your function here:

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

            }
        }
    });
});

add regex in Jquery.validate

Upvotes: 4

Related Questions