最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Issue using remote method in jquery validation - Stack Overflow

programmeradmin0浏览0评论

I'm following this article in order to validate my form

My problem is when I have to use the remote method, e.g. remote: "check-username.php"

Since the documentation for remote method is not so clear for me, I would know:

How I need to structure my json output in php script?

I'm following this article in order to validate my form

My problem is when I have to use the remote method, e.g. remote: "check-username.php"

Since the documentation for remote method is not so clear for me, I would know:

How I need to structure my json output in php script?

Share Improve this question edited Sep 24, 2013 at 7:49 GVillani82 asked Apr 25, 2013 at 11:36 GVillani82GVillani82 17.4k32 gold badges109 silver badges176 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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";

Invalid (failure):

echo "false"; // this can be any false. eg: 0 , "" , NULL.

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')
    }
}

You don't need JSON. You can put any text. For example you can

echo "success";

for passed validation, and enter validation message like:

echo "Username already exists.";

for failed validation.

In your callback do something like this:

remote: {
           url: "check-username.php" ,
           type: "post" ,
           data: {
              username: function() {
              return $("#username").val();
           },
           complete: function(data){
                if( data.responseText != "success" ) {
                   alert(data.responseText);
                   //handle failed validation
                }
             }
         }
发布评论

评论列表(0)

  1. 暂无评论