以下功能完美无缺。目前,如果用户名不可用,我正在提示“已注册电子邮件ID - 请登录”。
我的查询是,而不是提醒,我可以将上面的消息添加到验证错误对象中,以便默认情况下将其选中errorPlacement函数(然后在jsp上显示错误消息 - 它通常用于其他验证失败的方式) ?
<script type="text/javascript">
$(document).ready(function(){
var formAction = '';
var userAvailable = '';
$("#myForm").validate({
onfocusout:false,
onkeyup:false,
onclick:false,
rules:{
j_username:{
required: true,
email: true,
minlength: 8
}
},
messages: {
j_username:{
required: "Please enter a valid email address",
minlength: "Please enter a valid email address"
}
},
// the errorPlacement has to take the table layout into account
errorPlacement: function(error, element) {
if(element.is(":radio") )
error.appendTo( element.parent().next());
else
error.appendTo( element.parent());
},
submitHandler: function(form){
if($('#accountNo').is(':checked')){
var emailId = $('#userName').val();
isUserNameAvailable(emailId);
if(userAvailable=="1"){
alert("Email ID already registered - please log in");
return false;
}
var loginAction = '/MyApp/articles/add.html';
$('#myForm').attr('method','POST');
$('#myForm').attr('action',loginAction);
form.submit();
}
else{
var loginAction = '/MyApp/j_spring_security_check';
$('#myForm').attr('method','POST');
$('#myForm').attr('action',loginAction);
form.submit();
}
}
});
//Ajax call to check userName availability returns '1' if userName is available
function isUserNameAvailable(id) {
$.ajax({
url: "isUserAvailable.html",
data: ({emailId : id}),
async: false,
success: function(data) {
userAvailable = data;
}
});
}
});
</script>
我想我可以使用addMethod()但不确定它。我所指的一个例子如下......
jQuery.validator.addMethod("math", function(value, element, params) {
return this.optional(element) || value == params[0] + params[1];
}, jQuery.format("Please enter the correct value for {0} + {1}"));
好的,我使用adMethod工作了..代码如下。
<script type="text/javascript">
$(document).ready(function(){
var formAction = '';
var response;
$.validator.addMethod("uniqueUserName", function(value, element) {
var id = $('#userName').val();
if($('#accountNo').is(':checked')){
$.ajax({
url: "isUserAvailable.html",
data: ({emailId : id}),
async: false,
success: function(msg)
{
//If username exists, set response to true
response = ( msg == '1' ) ? true : false;
}
});
return response;
}
else{
return true;
}
}, "Username is Already Taken");
$("#myForm").validate({
onfocusout:false,
onkeyup:false,
onclick:false,
rules:{
j_username:{
required: true,
email: true,
minlength: 8,
uniqueUserName: true
}
},
messages: {
j_username:{
required: "Please enter a valid email address",
minlength: "Please enter a valid email address",
uniqueUserName: "Email ID already registered - please log in"
}
},
// the errorPlacement has to take the table layout into account
errorPlacement: function(error, element) {
if(element.is(":radio") )
error.appendTo( element.parent().next());
else
error.appendTo( element.parent());
},
submitHandler: function(form){
if($('#accountNo').is(':checked')){
var loginAction = '/MyApp/articles/add.html';
$('#myForm').attr('method','POST');
$('#myForm').attr('action',loginAction);
form.submit();
}
else{
var loginAction = '/MyApp/j_spring_security_check';
$('#myForm').attr('method','POST');
$('#myForm').attr('action',loginAction);
form.submit();
}
}
});
});