0

I need a jquery function to validate my textbox value.

Requirement: required filed:true, minlength:8, maxlength:16, should allow only alphabets, numbers.

i have written one function:

jQuery.validator.addMethod("nameRegex", function (value) {
      //  alert(value);
        if (value.length >= 8) {
           // alert(value.length); 
                return value.match(/^[a-zA-Z0-9 ]+$/.test(value)); 
        }
    }, "Contain only letters, numbers."); 

but this is not working can any one pls help me in this.

-Sindhu.A

1
  • 1
    "is not working" explains nothing. What actually doesn't work? Is it even triggered? Commented Apr 5, 2012 at 5:11

1 Answer 1

0

Hiya hope this help :)) Working Demo : http://jsfiddle.net/2FByp/2/

Like @zerkms said please be very clear what is not working. you can always refer to the validation plugin documentation && this might help you as well: using the jquery validation plugin, how can I add a regex validation on a textbox?

NOTE Please make sure all the rules are added correctly and compare your implementation with the jsfiddle above, else flick me jsfiddle I might be help BUT this js fiddle will help you to find out why it is not working. :)

HTML

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript" src="http://jquery.bassistance.de/validate/jquery.validate.js"></script>

<script type="text/javascript">

</script>

<form method="post" id="myForm">

    Name regex (alphanumeric):<input type="text" name="name" />  
    <input type="submit" value="Go" />  
</form>​

JQuery Code

  $(function() {

    $.validator.addMethod("nameRegex", function(value, element) {
        return this.optional(element) || /^[a-z0-9\-]+$/i.test(value);
    }, "Username must contain only letters, numbers, or dashes.");

    $("#myForm").validate({
        rules: {
            "name": {
                required: true,
                nameRegex: true,
                minlength: 8

            }
        },
        messages: {
            "name": {
                required: "You must enter a login name",
                nameRegex: "Contain only letters, numbers."
            }
        }
    });

});​

Hope this is helpful cheers!

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

Comments

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.