1

I'm needing to write my own javascript rules to validate a form.

I have a field named code and I need a special validation to it: This field have exactly 13 positions. The first and second are letters and the last and penultimate one are letters too. The other positions (3-11) are numbers. So, how can I validate this field with these conditions?

<html>
  <head>
    <script type="text/javascript" 
      src="javascript/jquery-1.8.2.min.js" >
    </script>
    <script type="text/javascript" 
      src="javascript/jquery.validate.min.js" >
    </script>
  </head>
  <body>
    <form id="meu_form" action="" method="post" >
      Code:<br />
      <input type="text" name="code" id="code" /><br />
      <input type="submit" value="Save" />
    </form>
  </body>
  <script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery('#meu_form').validate({
            rules:{
                code:{
                    required: true,
                    minlength: 13
                    maxlength: 13
                }
            },
            messages:{
                code:{
                    required: "Required field.",
                    minlength: "Exactly 13 length."
                }
            }

        });
    });
  </script>
</html>
j08691
  • 204,283
  • 31
  • 260
  • 272
GarouDan
  • 3,743
  • 9
  • 49
  • 75

1 Answers1

1

Use a RegEx:

/^[A-Z]{2}[0-9]{10}[A-Z]$/i
Louis Ricci
  • 20,804
  • 5
  • 48
  • 62
  • Very interesting. But how can I do this with Javascript. I will search a litle after (now I cant), but if is easy to you, please write some code to me. Thx a lot. – GarouDan Oct 22 '12 at 19:58
  • With this [question](http://stackoverflow.com/questions/280759/jquery-validate-how-to-add-a-rule-for-regular-expression-validation/709358#709358) and your regex works. Thx a lot. – GarouDan Oct 23 '12 at 15:53