slug: function (value, element) {
return this.optional(element) || /^[A-Za-z0-9-]+$/.test(value);
},
这是我的字母,数字和连字符验证功能,但现在我想添加。(点),所以函数可以接受。(点),我该怎么办
答案 0 :(得分:1)
无需自定义规则来执行此操作,您可以使用additional-methods.js文件中的模式规则,例如
jQuery(function($) {
var validator = $('#myform').validate({
rules: {
somefield: {
pattern: /^[A-Za-z\d-.]+$/,
required: true
}
},
messages: {}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/jquery.validate.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/additional-methods.js"></script>
<form id="myform" method="post" action="">
<input name="somefield" />
<input type="submit" value="Save" />
</form>