这是我的表格(它只包含本练习所需的字段,实际上有 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 ,因此,已经有一个具有该用户/密码组合的用户。
但是,从未显示验证消息。我相信我已经按照正确的方法使用函数调用进行验证。我不能使用 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 函数以成功调用另一个函数。