a-z正则表达式在jQuery验证中不起作用

时间:2016-02-10 11:02:36

标签: jquery regex

我的代码看起来像这样。帮帮我

$('#form').validate({
  errorElement: "span", // contain the error msg in a span tag
  errorClass: 'help-block',
  ignore: "",
  rules: {
    CountryName: {
      required: true,
      regex: /^[a-zA-Z]+$/
    }
  },
  messages: {
    CountryName: {
      required: "Enter Country name",
      regex: "Enter Correct Country name",
    },
  },

1 个答案:

答案 0 :(得分:2)

没有regex规则,它被称为pattern,来自additional-methods文件



jQuery(function($) {
  $('#form').validate({
    errorElement: "span", // contain the error msg in a span tag
    errorClass: 'help-block',
    ignore: "",
    rules: {
      CountryName: {
        required: true,
        pattern: /^[a-zA-Z]+$/
      }
    },
    messages: {
      CountryName: {
        required: "Enter Country name",
        pattern: "Enter Correct Country name",
      }
    }
  });
});

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/jquery.validate.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/additional-methods.js"></script>
<form id="form" method="post" action="">
  <input name="CountryName" />
  <input type="submit" value="Save" />
</form>
&#13;
&#13;
&#13;