是否可以将jquery.validate错误代码写入localstorage? [英] Is it possible to write jquery.validate error codes into localstorage?

查看:94
本文介绍了是否可以将jquery.validate错误代码写入localstorage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这是否可能.因此,我将向所有对此领域有知识的专业人员介绍此内容.是否可以将jquery.validation错误代码写入localstorage?我在我的大部分网站上都使用jquery.validation.如果用户输入了错误的电子邮件,则会弹出错误请输入有效的电子邮件".如果用户刷新页面,错误的电子邮件仍会保留,但弹出错误将不再存在.是否有任何专业人士知道这是否可行?如果可以,我该怎么做?

I am not sure if this is even possible. So I am presenting this to any professionals with knowledge in this area. Is it possible to write the jquery.validation error codes into localstorage? I am using jquery.validation for much of my site. If a user inputs an incorrect email, the error "please put in a valid email" pops-up. If the user refreshes the page the incorrect email still remains but the pop-up error no longer exists. Are there any professionals that know if this is possible, and if so how can I do this?

我已经成功获得了验证码,可以在我的所有表格上使用.但是,在没有适当知识的情况下,我不愿意处理脚本的源代码.

I have successfully got the validation code to work on all my forms. However I am not comfortable manipulating the script's source code without proper knowledge.

    // validate signup form on keyup and submit
    $("#email_preference").validate({
        rules: {
            firstname: "required",
            lastname: "required",
            username: {
                required: true,
                minlength: 2
            },
            password: {
                required: true,
                minlength: 5
            },
            confirm_password: {
                required: true,
                minlength: 5,
                equalTo: "#password"
            },
            my_email: {
                required: true,
                email: true
            },
            topic: {
                required: "#newsletter:checked",
                minlength: 2
            },
            agree: "required"
        },
        messages: {
            firstname: "Please enter your firstname",
            lastname: "Please enter your lastname",
            username: {
                required: "Please enter a username",
                minlength: "Your username must consist 
                of at least 2 characters"
            },
            password: {
                required: "Please provide a password",
                minlength: "Your password must be at 
                least 5 characters long"
            },
            confirm_password: {
                required: "Please provide a password",
                minlength: "Your password must be at 
                least 5 characters long",
                equalTo: "Please enter the same 
                password as above"
            },
            my_email: "Please enter a<br>valid 
            email address",

            confirm_password: {
                required: "Please provide a password",
                minlength: "Your password must be at 
                least 5 characters long",
                equalTo: "Please enter the same 
                password as above"
            },
            agree: "Please accept our policy",
            topic: "Please select at least 2 topics"
        }
    });

我在页面上附加了选项代码.脚本的源代码太长,无法粘贴到此处.但是,这是脚本的链接. https://jqueryvalidation.org/ 如果可能的话,我希望一旦页面上出现弹出错误,就会将来自错误电子邮件的错误代码写入到本地存储中,因此将在刷新页面后显示.如果电子邮件已更正,我希望删除本地存储条目.对于少数知道如何执行此操作的人,我要提前谢谢您.

I have attached the options code from my page. The script's source code is too long to paste here. However, this is the link to the script. https://jqueryvalidation.org/ If possible I would like the error code from an incorrect email to be written into localstorage as soon as the popup error appears on the page, so it will appear after a possible page refresh. If the email is corrected I would like for the localstorage entry to be removed. To the few that know how to do this, I thank you ahead of time.

推荐答案

jquery.validate(Brahim Arkni.)的作者建议我使用高亮功能,以便在触发错误时设置localstorage项目.使用高亮显示的好处是,您可以在不突出显示的情况下触发删除本地存储项.因此,执行此部分的代码(由Brahim提供)是给定的.

The author of jquery.validate (Brahim Arkni.) suggested I use the highlight function in order to set the localstorage item as the error is triggered. The nice thing about using highlight is that you can trigger the removal of the localstorage item with unhighlight. So the code to do this part (supplied by Brahim) is as given.

//'''
highlight: function(element, errorClass, validClass) {
        // Check for your email input
            if (element.type === "email" && element.name === "input_name") {
            // `errorMap` is an internal Key/value store, where the key refers to the
            // name of an input field, values the message to be displayed for that
            // input.
            localStorage.setItem("email_error", this.errorMap[element.name]);
            }

            // Call the default highlight to keep the original behaviour
            $.validator.defaults.highlight(element, errorClass, validClass);
        },
          unhighlight: function(element, errorClass, validClass) {
            // Check for your email input
            if (element.type === "email" && element.name === "input_name") {
              localStorage.removeItem("email_error");
            }

现在,为了在页面刷新或浏览器崩溃时调用该代码,您可以使用此代码.

Now in order to call the code on a page refresh or a browser crash you can use this code.

    // Show the error at startup if it was already set
    if (localStorage.hasOwnProperty("email_error")) {
        $("div_holding_input").after('<span id="id_given_by_jquery.validate-error" class="error">Please enter a<br>valid email address</span>');
    };
    $('#input_id').click(function() {            
        $("#id_given_by_jquery.validate-error").remove();
        localStorage.removeItem("email_error");
    });

您正在输入jquery.validate生成的整个范围,以显示在页面刷新上.一旦用户再次开始键入,您将需要删除整个范围和本地存储,因为jquery.validate将初始化并向页面添加第二个错误.我不是专家,但是我确实在进行此工作以使其正常运行.如果有人可以使代码更好,那么我一定会喜欢学习的.谢谢

You are inputting the entire span that is produced by jquery.validate to show on page refresh. You will need to delete the entire span and the localstorage once the user starts typing again because jquery.validate will initialize and add a second error to the page. I am not an expert but I did work on this to get it to function properly. If anyone can make the code better I would definitely love to learn. Thanks

这篇关于是否可以将jquery.validate错误代码写入localstorage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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