这是我的表单(它只包含本练习所需的字段,实际上有20个输入字段):
<form id="reg_form" action="/register_submit" method="post">
<fieldset>
<ul class="form-list">
<li>
<div style="width:500">
<label for="Username" class="required">Username (5 characters minimum,<br />Only Letters, Numbers, '~', '%', '.', ':', '-' and '_' allowed)<em>*</em></label><br />
<div class="input-box">
<input type="text" name="Username" id="Username" value="{embed:Username}" title="Username" class="validate[required,minSize[5],custom[onlyLetNumSpec]] register_input" />
</div>
</div>
</li>
<li id="register-customer-password">
<div style="width:500">
<label for="Password" class="required">Password (5 characters minimum,<br />Only Letters, Numbers, '~', '%', '.', ':', '-' and '_' allowed)<em>*</em></label><br />
<div class="input-box">
<input type="Password" name="Password" id="Password" title="Password" class="validate[required,minSize[5],custom[onlyLetNumSpec]] register_input" />
</div>
</div>
<br />
<div style="width:500">
<label for="confirm_password" class="required">Confirm Password<em>*</em></label>
<div class="input-box">
<input type="password" name="confirm_password" title="Confirm Password" id="confirm_password" class="validate[required,custom[onlyLetNumSpec],equals[Password],funcCall[checkUserPass]] register_input" />
</div>
</div>
<div class="clear"></div>
</li>
</ul>
<div class="button-set">
<button class="button button-login" title="Create Account" type="submit">
<span>
<span>Create Account</span>
</span>
</button>
</div>
</fieldset>
</form>
现在,这是在完成并退出confirm_password表单字段后在验证时调用的函数checkUserPass:
function checkUserPass(field, rules, i, options){
$.ajax({type:'POST',
url: '/json_api/ParticipantSelectByUser/',
data: {"Username":$("#Username").val(), "Password":$("#Password").val()},
dataType: 'json',
success: checkUserPassResult,
error:function (xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
} // error
});
}
function checkUserPassResult(data) {
if(data.result.ParticipantID > 0) {
/*--- There is already a user with this combination ---*/
return options.allrules.checkUserPass.alertText;
} // if(data.result)
}
我知道返回了data.result,因此,已经有一个用户拥有该用户/ pwd组合。
但是,从未显示验证消息。我相信我已经按照正确的方式通过函数调用进行验证。我不能使用ajax验证规则,因为需要将用户名和密码都传递给ajax调用。这里似乎有什么问题?我在这个表单上的所有其他验证工作正常。
以下是上述验证所需的自定义规则:
"onlyLetNumSpec": {
// Good for database fields
"regex": /^[0-9a-zA-Z_-~%.:]+$/,
"alertText": "* Only Letters, Numbers, tilde(~), pct(%), period(.), colon(:), hyphen(-) and underscore(_) allowed"
},
"checkUserPass": {
"alertText": "* A User with the same Username and/or Password has been detected. Please change one of the entries."
}
编辑说明;我更新了checkUserPass函数,以便成功调用另一个函数。
答案 0 :(得分:1)
确定。我找到了解决方案!您所要做的就是从主要功能返回而不是回叫。
function checkUserPass(field, rules, i, options){
var ret = "";
$.ajax({type:'POST',
url: '/json_api/ParticipantSelectByUser/',
data: {"Username":$("#Username").val(), "Password":$("#Password").val()},
dataType: 'json',
async: false,
success: function(response)
{
if(data.result.ParticipantID > 0)
{
ret = "There is already a user with this combination";
}
},
error:function (xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
} // error
});
**return ret;**
}
答案 1 :(得分:0)
您正在使用异步ajax,因此无法返回您的返回值。您必须使用数据调用函数。而且您必须在ajax函数之外使用e.preventDefault()(!!!)来阻止提交。
if(data.result) {
function_to_validate_the_data(data.result);
} // if(data.result)