我使用Validate.Js使用 jQuery 验证我的 HTML 表单,它运行良好。但是我需要在每个字段旁边显示错误,而不是在页面顶部显示所有错误的摘要。
HTML:
<div class="success_box">All of the fields were successfully validated!</div>
<div class="error_box"></div>
<form name="example_form">
<label for="req">Required field:</label>
<label for="alphanumeric">Alphanumeric field:</label>
<div id="AlphaNumeric"></div>
<input name="req" id="req" />
<input name="alphanumeric" id="alphanumeric" />
<label for="password">Password:</label>
<label for="password_confirm">Password Confirmation (match password):</label>
<input name="password" id="password" type="password" />
<input name="password_confirm" id="password_confirm" type="password" />
<label for="email">Email:</label>
<label for="minlength">Min length field (min. 8 chars):</label>
<input name="email" id="email" />
<input name="minlength" id="minlength" />
<label for="tos_checkbox">Required checkbox (example: Terms of Service)</label>
<input name="tos_checkbox" id="tos_checkbox" type="checkbox" />
<button class="button gray" type="submit" name="submit">Submit</button>
</form>
JavaScript:
new FormValidator('example_form', [{
name: 'req',
display: 'required',
rules: 'required'
}, {
name: 'alphanumeric',
rules: 'alpha_numeric'
}, {
name: 'password',
rules: 'required'
}, {
name: 'password_confirm',
display: 'password confirmation',
rules: 'required|matches[password]'
}, {
name: 'email',
rules: 'valid_email'
}, {
name: 'minlength',
display: 'min length',
rules: 'min_length[8]'
}, {
name: 'tos_checkbox',
display: 'terms of service',
rules: 'required'
}], function(errors, event) {
var SELECTOR_ERRORS = $('.error_box'),
SELECTOR_SUCCESS = $('.success_box');
if (errors.length > 0) {
SELECTOR_ERRORS.empty();
for (var i = 0, errorLength = errors.length; i < errorLength; i++) {
SELECTOR_ERRORS.append(errors[i].message + '<br />');
}
SELECTOR_SUCCESS.css({ display: 'none' });
SELECTOR_ERRORS.fadeIn(200);
} else {
SELECTOR_ERRORS.css({ display: 'none' });
SELECTOR_SUCCESS.fadeIn(200);
}
if (event && event.preventDefault) {
event.preventDefault();
} else if (event) {
event.returnValue = false;
}
});
有没有办法改变显示风格?有没有更好的脚本可以做到这一点?