我一直在努力让jquery-validation在我的项目中工作,但它似乎并没有将自己附加到我的 jquery 对象上。
我的 package.json 看起来是:
{
"name": "web01",
"version": "1.0.0",
"dependencies": {
"jquery": "2.1.3",
"pathjs": "0.8.1",
"jquery-validation" : "1.13.1"
},
"devDependencies": {
"browserify": "8.0.2",
"browserify-mustache": "0.0.4",
"browserify-shim": "3.8.2",
"phantomjs": "1.9.13",
"mocha-phantomjs": "3.5.2",
"mocha": "2.1.0",
"sinon": "1.12.2",
"proxyquireify": "1.1.0"
},
"browserify": {
"transform": [
"browserify-mustache",
"browserify-shim"
]
},
"browserify-shim": {
"jquery" : "$",
"jquery-validation": {
"exports": "null",
"depends": [ "jquery" ]
}
}
}
npm install 一切正常,但是当我尝试在 js 文件中调用 validate 时,我只是收到一个错误,提示该函数未定义。我已经查看了大约六个 stackoverflow 问题,但它们并没有解决我的问题。我本来希望 validate 已添加到 jquery 对象中,但不确定是否应该在我的 js 文件中调用 require('jquery-validation') 。
var $ = require('jquery');
var ajax = require('./ajax.js');
var constraints = {
rules: {
email: {
required: true,
email: true
},
displayName: "required",
password: {
required: true,
minlength: 6,
pattern: "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).+$"
}
},
messages: {
required: "Email not provided",
email: "this ain't no email"
},
displayName: {
required: "display name required"
},
password: {
required: "password required",
minlength: "not long enough",
pattern: "isn't complicated enough"
},
submitHandler: function($form) {
var data = extractData($form);
ajax.send({
type: 'POST',
url: '/register',
data: data,
error: function() {
console.log('failed to register');
}
});
function extractData($form) {
return {
email: $form.find('#email').val(),
displayName: $form.find('#displayName').val(),
password: $form.find('#password').val()
};
}
}
};
function registration(e) {
e.preventDefault();
$('#registration-form').validate(constraints);
}
module.exports = {
selector: '#registration-form',
registration: registration
};
任何人都有类似的问题/关于如何解决这个问题的任何想法?我可以使用 validate.js,但如果可能的话,我更喜欢使用 jquery 插件。
谢谢