5
我有一種表單從最終用戶收集一些個人信息並觸發一些JS驗證功能。對於簡單的示例,我們只需說表單中包含名字,姓氏和電子郵件地址。如何讓jQuery驗證以驗證由瀏覽器預填充的字段
現在一旦表單被填寫並提交,如果我回到它,我的瀏覽器會預先填寫表單,就像您期望的那樣。問題是當我去提交(沒有改變任何字段或通過它們)時,插件不會返回並驗證這些字段(如果它們已經預先填充了。)
我不知道爲什麼它沒有驗證。?。在預填充字段和我不知道如何得到它沒有人有任何想法我運行jQuery的最新版本和驗證插件(http://jqueryvalidation.org/)
示例代碼:
$(document).ready(function() {
var rules = {
FirstName: 'required',
LastName: 'required',
EmailAddress: {
required: true,
customEmail: true,
checkAccountExists: true
}
};
//And field specific (and even validation type specific) error messages
var messages = {
FirstName: 'Your first name is required.',
LastName: 'Your last name is required.',
EmailAddress: {
required: 'Your email address is required.',
customEmail: 'You must enter a valid email address.',
checkAccountExists: 'We already have an account with that email address. Please login.'
}
};
$('#applicationForm').validate({
//debug: true,
rules: rules,
messages: messages,
errorElement: 'span'
});
});
jQuery.validator.addMethod('customEmail', function(value, element) {
return this.optional(element) || /[A-z0-9._%-+]{1,}@[A-z0-9._%-]{1,}\.[A-z0-9._%-]{1,}/.test(value);
}, 'Invalid email address entered.');
jQuery.validator.addMethod('checkAccountExists', function(value, element) {
if (this.optional(element)) {
return true;
}
var url = $(element).attr('checkEmailUrl');
$.ajax({
type: 'GET',
data: {EmailAddress: value, check: true},
dataType: 'json',
url: url,
success: function(response) {
var dataArray = jQuery.parseJSON(response);
//If it exists then trigger the popup
if (dataArray.result == 'EXISTS') {
kclHelpers.showEmailExistsModal(value);
}
}
});
return true; //If it exists the popup will handle it. We are just using this to trigger it
}, 'An account under the specified email address already exists. Please sign in.');
做任何的這些幫助:http://stackoverflow.com/questions/3797455/jquery-validation-plugin-how-can-i-force-validation-on-previously-valid-fields; http://stackoverflow.com/questions/7057308/jquery-form-validation-issue – acarlon