简体   繁体   English

编写自定义验证规则

[英]Writing a custom validation rule

I'm needing to write my own javascript rules to validate a form. 我需要编写自己的JavaScript规则来验证表单。

I have a field named code and I need a special validation to it: This field have exactly 13 positions. 我有一个名为code的字段,我需要对其进行特殊验证:该字段正好有13个位置。 The first and second are letters and the last and penultimate one are letters too. 第一个和第二个是字母,最后一个和倒数第二个也是字母。 The other positions (3-11) are numbers. 其他位置(3-11)是数字。 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>

使用正则表达式:

/^[A-Z]{2}[0-9]{10}[A-Z]$/i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM