【发布时间】:2015-04-09 15:14:23
【问题描述】:
在编辑页面上,我使用http://jqueryvalidation.org/ 来验证表单。
//validate object set using external file linked to page
var myValidateObj = {
"rules": {
"foo": {"required": true,"maxlength": 45,"minlength": 2},
"bar": {"maxlength": 45},
},
"messages": {
"foo": {"required": "Foo is required.",},
"bar": {"maxlength": "bla bla."},
}
};
var validator=$("#myform").validate({
rules: myValidateObj.rules,
messages: myValidateObj.messages,
});
<form id="myForm">
Foo: <input type="text" name="foo" />
Bar: <input type="text" name="bar" />
<input type="submit" value="SAVE">
</form>
在另一个页面上,我正在显示之前描述的编辑页面的值,并允许使用http://vitalets.github.io/x-editable/ 进行内联编辑。验证规则和消息仍然适用,但现在只需要验证单个字段。假设此页面上存在myValidateObj 对象,是否可以使用它来验证单个字段?例如,在可编辑的验证回调中,如何使用myValidateObj 验证foo 并在未通过验证时返回适当的消息?
$('#foo').editable({
type: 'text',
validate: function(value) {
//How do I use myValidateObj to validate foo and return applicable message???
}
});
Name: <a href="javascript:void(0)" id="foo"><?php echo($foo); ?></a>
Bla: <a href="javascript:void(0)" id="bar"><?php echo($bar); ?></a>
【问题讨论】:
标签: javascript jquery jquery-validate x-editable