7

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...

3
  • try regex: '^[0-9]+$' Commented Jan 6, 2014 at 9:28
  • The plugin doc site seems down but I suspect the plugin needs a regex, not a string. Can you try with a regex literal : regex: /^[0-9]$/ ? EDIT : found the source code, shouldn't matter. Commented Jan 6, 2014 at 9:30
  • Can you build a fiddle so that we can reproduce your problem ? Commented Jan 6, 2014 at 9:34

4 Answers 4

15

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/

Sign up to request clarification or add additional context in comments.

4 Comments

How would you explain the fact that 2 wasn't seen as valid ?
@dystroy As you suspected in your comment , the plugin expect a regular expression not a string.
@dystroy check the demo in my answer.
Well my problem is regex, and obviously I have one method already added for regex. Now it is working...thanks
4

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

Comments

0

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.

Comments

-1

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"
        }
    }
});

1 Comment

Uncaught TypeError: Cannot read property 'call' of undefined. Exception occurred when checking element zipcode, check the 'pattern' method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.