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

user1314384
  • 1
  • 1
  • 1

1 Answers1

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!

Community
  • 1
  • 1
Tats_innit
  • 33,991
  • 10
  • 71
  • 77