使用jquery validate插件返回带有远程的字符串 [英] returning a string with remote using the jquery validate plugin

查看:132
本文介绍了使用jquery validate插件返回带有远程的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从jquery文档中,描述了jquery validate插件的远程功能:

使用默认消息,该响应被评估为JSON,对于有效元素必须为true,对于无效元素可以为false,undefined或null.或字符串,例如错误消息中将显示该名称已被使用,请尝试使用peter123".

From the jquery documentation, describing the remote function of the jquery validate plugin:

The response is evaluated as JSON and must be true for valid elements, and can be any false, undefined or null for invalid elements, using the default message; or a string, eg. "That name is already taken, try peter123 instead" to display as the error message.

我有一个PHP页面,它是echo的响应,并且如果我使用echo("true")或echo("false"),它的工作方式如上所述.但是,每当我回显字符串时,都不会显示错误消息,甚至不会显示默认消息.我该怎么做以回显错误消息,并将其显示在经过验证的输入框旁边的错误标签中?

I have a php page that echo's a response, and it works as described if I use echo("true") or echo("false"). However, whenever I echo a string, no error message is displayed, not even the default message. What must I do to echo back an error message and have it display in the error label next to the input box being validated?

这是我的jquery函数:

here is my jquery function:

$(document).ready(function(){
    $("#masq").validate({ 
        rules: { 
             user: {
                required:true,
                minlength:1,
                remote:"<?=$_SERVER['PHP_SELF']?>?action=remoteCheck"
             }
        }, 
        messages: { 
            user: "id must have at least 1 character."
        } 
    });         
 });

和我的php函数:

//sql validation here
    if(!$user) {
        $return = "id " . $user . " does not exist."; 
        //echo("false"); works correctly
            echo($return); //does  
    }
    else {
        echo("true");
    }

推荐答案

我也为此感到困惑(我需要向其传递一个以上的值,并根据哪些条件返回false来显示级联的错误消息,但这仅用于另一个SO发布),但是就像我最初所做的那样,您会误解文档中所说的内容-它只能返回true或false(因为它以JSON格式传递)-所指的字符串"是如果您使用消息:代码,然后指定与您的错误相关的消息:

I struggled with this too (I need to pass more than one value to it, and depending on which conditions return false, show concatenated error messages, but that's for another SO posting) , but like I did initially, you are misunderstanding what the documentation says - it can ONLY return true or false (because it's being passed in JSON format) - the "string" it is referring to is if you use the messages: code, and then specify a message related to your error:

这是一个示例(带有注释):

Here's an example (with comments):

$("#MemberInfo").validate({
    errorPlacement: function(error, element) {
           error.insertAfter(element);
       },
    rules: {    
        Memberemail: {
            required: true,
            email: true,
            remote: "checkReg.cfm"
        },

    },
    messages: {
        Memberemail: {
            required: "Please enter a valid email address",
            remote: jQuery.validator.format("{0} is already registered")    
        }
    }
});

服务器端代码返回"true"或"false",并且消息中的{0}将值从表单传递到服务器端脚本,并打印出该值以及其余的自定义错误消息出现在该字段旁边的错误中.

The server side code returns "true" or "false", and the {0} in the message puts the value passed to the server-side script from the form, and prints out that plus the rest of the custom error message in the error that appears next to the field.

我仍在学习JQuery,所以希望这对您或其他任何人有帮助!

I'm still learning JQuery, so I hope this helps you or anyone else out, if it's still an issue for you!

这篇关于使用jquery validate插件返回带有远程的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆