jQuery验证错误返回链接 [英] jQuery Validation Error Return a Link

查看:55
本文介绍了jQuery验证错误返回链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jquery验证插件.我有一个电子邮件字段.如果电子邮件在使用中,我希望验证插件给出错误,并提供指向客户端页面的链接.

I am using jquery Validation Plugin. I have an email field. If the email is in use, I want the validation plugin to give an error with a link to the client's page.

IE:如果我输入电子邮件john@bobo.com,并且他是客户#312,名称为John Mark,我希望我的错误是:

IE: if I input the email john@bobo.com and he is client #312 with the name John Mark, I want my error to be:

<a href="/index.php?client=312">John Mark</a> is using that email.

最好我想让我的外部文件回显整个错误,并让jQuery Validation插件显示该完整错误.如果没有,我想同时返回客户端名称和客户端ID,然后能够输出带有链接的错误消息.

Preferably I would like to have my external file just echo the entire error and have the jQuery Validation plugin to display that full error. If not, I would like to have both the client name and client id to be returned and then be able to output the error message with a link.

jQuery代码:

$().ready(function() {
    // Validate the form
    $('#sign-up_area').validate({
      rules: {
        firstName: "required",
        lastName: "required",
        email_address: {
        email: true,
        remote: {
            url: "includes/databasecheck.php",
            type: "post",
            success: function(html){
                $("#email").html(html);
            }
        }
        }
    },
    messages: {
        firstName: "First Name Required",
        lastName: "Last Name Required",
        email_address: {
        email: "Email address need to be valid.",
        //remote: jQuery.format("{0} is taken")
        },
    }
    });
});

表格:

<form action="#" method="post" id="sign-up_area">

    <h2 id="reference" name="reference" class="heading-reference">Client</h2>

                <fieldset>
                    <label for="firstName">First Name:</label>
                    <input type="text" name="firstName" id="firstName" value="">
                </fieldset>

                <fieldset>
                    <label for="lastName">Last Name:</label>
                    <input type="text" name="lastName" id="lastName" value="">
                </fieldset>

        <fieldset>
                    <label for="email_address">Email:</label>
                    <input type="text" name="email_address" id="email_address" value="">
            <div id="email" class="error"></div>
                </fieldset>
            <fieldset class="form-actions">
                <input type="submit" value="Submit">
            </fieldset></form>

databasecheck.php

databasecheck.php

<?php
include_once("config.php"); // database connection

if(isset($_POST['email_address'])) {
    $stmt = $mysqli->prepare("SELECT client_id, CONCAT(firstName, ' ', lastName) AS whole_name FROM client WHERE email = ? LIMIT 1"); 
    $stmt->bind_param('s', $_POST['email_address']);
    $stmt->execute(); // Execute the prepared query.
    $stmt->store_result();
    $stmt->bind_result($client_id, $whole_name);
    $stmt->fetch();
    echo '<a href="/index.php?client='.$client_id.'">'.$whole_name.'</a> is using that email.';
}
?>

推荐答案

根据远程方法的文档,则错误消息可能只是您从服务器返回的内容.

As per the documentation for the Remote Method, the error message can just be whatever you return from your server.

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

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.

只要您在服务器上将自定义错误消息构造为有效的JSON,您的代码就可以正常使用,并且可以删除远程消息...

As long as you construct the custom error message on the server as valid JSON, your code is fine with the message for remote removed...

rules: {
    firstName: "required",
    lastName: "required",
    email_address: {
        email: true,
        remote: {
            url: "includes/databasecheck.php",
            type: "post",
            success: function(html){
                $("#email").html(html);
            }
        }
    }
},
messages: {
    firstName: "First Name Required",
    lastName: "Last Name Required",
    email_address: {
        email: "Email address need to be valid."
        // The remote error message is coming from the server automatically
        // remote: jQuery.format("{0} is taken")  // <- REMOVE
    },
}

尝试使用此PHP:

if(isset($_POST['email_address'])) {
    $stmt = $mysqli->prepare("SELECT client_id, CONCAT(firstName, ' ', lastName) AS whole_name FROM client WHERE email = ? LIMIT 1"); 
    $stmt->bind_param('s', $_POST['email_address']);
    $stmt->execute(); // Execute the prepared query.
    $stmt->store_result();
    $stmt->bind_result($client_id, $whole_name);
    $stmt->fetch();
    $response = '<a href="/index.php?client='.$client_id.'">'.$whole_name.'</a> is using that email.';
    echo json_encode($response);  // failed validation- show the message
} else {
    echo "true";  // passed validation- no message
}

请参阅: http://php.net/manual/en/function. json-encode.php

这篇关于jQuery验证错误返回链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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