如何将jQuery验证方法分离到一个文件/类中



如何在单独的文件/类中分离jQuery验证方法。

$.validator.addMethod("specialChrs",function(element, value) {
        return this.optional(element) || /^[A-Za-zd= ]+$/.test(value)
}, "Special Characters not permitted");

我在许多文件中对不同的表单进行了这种验证。我怎样才能把separategeneric放在一个class中。谁能给我推荐一些设计模式,而分离jQuery函数使用?

你可以把JQuery放在一个单独的.js文件中,并在你的html文件的头部部分用script标签链接到它。

分隔.js文件

$(document).ready(function () {
    $.validator.addMethod("specialChrs",function(element, value) {
        return this.optional(element) || /^[A-Za-zd= ]+$/.test(value)
    }, "Special Characters not permitted");
});

你的html文件

<head>
    <script src="../yourjquery.js" type="text/javascript"></script>
</head>

也可以看看这个链接

相关内容

最新更新