在jquery验证中使用远程方法的问题

GVi*_*i82 3 javascript validation ajax jquery jquery-validate

我正在关注这篇文章以验证我的表单

我的问题是当我必须使用该remote方法时,例如remote: "check-username.php"

由于远程方法的文档对我来说不是那么清楚,我会知道:

我需要如何在PHP脚本中构建我的json输出?

man*_*nta 15

To use the default method for remote, the remote page must respond with a string value of "true" or "false".

Valid (success) Must be:

echo "true";
Run Code Online (Sandbox Code Playgroud)

Invalid (failure):

echo "false"; // this can be any false. eg: 0 , "" , NULL.
Run Code Online (Sandbox Code Playgroud)

The response will be evaluated as JSON.

To apply an error message, other than the default for a remote response of invalid ("false"), add the input name to the validator options messages object like so.

rules:{
    username:{
        remote: "check-username.php"
    }
},   
messages:{
    username:{
        remote: jQuery.format('{0} is already in use, please choose a different name')
    }
}
Run Code Online (Sandbox Code Playgroud)