如果输入字段无效,则清除该输入字段

发布于 2024-10-02 09:31:41 字数 540 浏览 0 评论 0 原文

我正在使用验证插件。我试图弄清楚如何清除文本字段(如果该字段无效)。

我的 JavaScript 代码如下,

$(document).ready(function() {
$("#enrollForm").validate({
    rules: {
        email: {
            required: true,
            email: true
        },
        vemail: {
            required: true,
            email: true,
            equalTo: "#email"
        },
        offer_code: {
            required: false,
            remote: "check_code.php"
        }
    }
});});

如果该字段无效,我会尝试清除名为 Offer_code 的字段。是否可以在验证函数中执行此操作?

亚历克斯

i am using validation plugin. i am trying to figure out how to clear a text field, if the field is invalid .

my javascript code is as follows

$(document).ready(function() {
$("#enrollForm").validate({
    rules: {
        email: {
            required: true,
            email: true
        },
        vemail: {
            required: true,
            email: true,
            equalTo: "#email"
        },
        offer_code: {
            required: false,
            remote: "check_code.php"
        }
    }
});});

I am trying to clear out the field called offer_code if the field is invalid. is it possible to do this with in the validate function?

Alex

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

盗琴音 2024-10-09 09:31:41

看看这个:

http://docs.jquery.com/Plugins/Validation/valid

例子:

$(document).ready(function() {
$("#enrollForm").validate({
    rules: {
        email: {
            required: true,
            email: true
        },
        vemail: {
            required: true,
            email: true,
            equalTo: "#email"
        },
        offer_code: {
            required: false,
            remote: "check_code.php"
        }
    }
});});

$("#enrollForm").blur(function() {
  if($("#enrollForm").valid()) {
    //success code?
  } else {
    $("#enrollForm").val('')
  }
});

Check this out:

http://docs.jquery.com/Plugins/Validation/valid

The example:

$(document).ready(function() {
$("#enrollForm").validate({
    rules: {
        email: {
            required: true,
            email: true
        },
        vemail: {
            required: true,
            email: true,
            equalTo: "#email"
        },
        offer_code: {
            required: false,
            remote: "check_code.php"
        }
    }
});});

$("#enrollForm").blur(function() {
  if($("#enrollForm").valid()) {
    //success code?
  } else {
    $("#enrollForm").val('')
  }
});
梦途 2024-10-09 09:31:41

我使用 jquery 来验证一些表单。例如,在我的网站上,有一个部分用户必须输入然后重新输入密码。如果他们的第二个密码的值等于他们的第一个密码,我使用 jquery 向下滑动一个 div,上面写着“您的密码匹配!”

试试这个:

$(document).ready(function(){
        $("#error").hide();
        $("#invite").hide();
        $("#confirm").hide();
        $("#invite_short").hide();
        $("#short").hide();

    $('.button').click(function checkPass(){
        var pass = $("#pass").val();
        var confirm = $("#con-pass").val();

        if(pass != confirm)
        {
            $("#error").slideDown();
            return false;
        } 

        });

    $('#con-pass').keyup(function checkPass(){
        var pass = $("#pass").val();
        var confirm = $("#con-pass").val();

        if(pass == confirm)
        {
            $("#error").slideUp();
            $("#confirm").slideDown();
            return false;
        } 

        if(pass != confirm)
        {
            $("#confirm").slideUp();
            return false;
        } 

        });

    $('#pass').keyup(function checkChar(){
        var pass = $("#pass").val().length;

        if(pass < 4)
        {
            $("#short").slideDown();
            return false;
        } 

        if(pass == 4
        || pass > 4)
        {
            $("#short").slideUp();
        } 

        });

        $('#invite_number').keyup(function checkCode(){
        var code = $("#invite_number").val();

        if(code.length == 8)
        {
            $("#invite_short").slideUp();
            $("#invite").slideDown();
            return false;
        }  

        if(code.length < 8)
        {
            $("#invite").slideUp();
            return false;
        }  


        });

        $('.button').click(function checkCode(){
        var code = $("#invite_number").val();

        if(code.length < 8)
        {
            $("#invite_short").slideDown();
            return false;
        }  

        });
    });

那里有很多代码。但是,它还根据长度要求验证了表单。如果密码少于 4 个字符,系统会在用户输入密码时发出通知。

你明白了......希望这有帮助

I have used jquery to validate a few forms. For instance, on my site, there is a part where a user has to enter and then re-enter their passwords. If the value of their second password is equal to their first password, I use jquery to slide down a div that says "Your passords match!"

Try this:

$(document).ready(function(){
        $("#error").hide();
        $("#invite").hide();
        $("#confirm").hide();
        $("#invite_short").hide();
        $("#short").hide();

    $('.button').click(function checkPass(){
        var pass = $("#pass").val();
        var confirm = $("#con-pass").val();

        if(pass != confirm)
        {
            $("#error").slideDown();
            return false;
        } 

        });

    $('#con-pass').keyup(function checkPass(){
        var pass = $("#pass").val();
        var confirm = $("#con-pass").val();

        if(pass == confirm)
        {
            $("#error").slideUp();
            $("#confirm").slideDown();
            return false;
        } 

        if(pass != confirm)
        {
            $("#confirm").slideUp();
            return false;
        } 

        });

    $('#pass').keyup(function checkChar(){
        var pass = $("#pass").val().length;

        if(pass < 4)
        {
            $("#short").slideDown();
            return false;
        } 

        if(pass == 4
        || pass > 4)
        {
            $("#short").slideUp();
        } 

        });

        $('#invite_number').keyup(function checkCode(){
        var code = $("#invite_number").val();

        if(code.length == 8)
        {
            $("#invite_short").slideUp();
            $("#invite").slideDown();
            return false;
        }  

        if(code.length < 8)
        {
            $("#invite").slideUp();
            return false;
        }  


        });

        $('.button').click(function checkCode(){
        var code = $("#invite_number").val();

        if(code.length < 8)
        {
            $("#invite_short").slideDown();
            return false;
        }  

        });
    });

There is a lot of code there. But, it also validates the form in terms of length requirements. If the password is less than 4 characters, it notifies them as they're entering their password.

You get the idea.... Hope this helps

计㈡愣 2024-10-09 09:31:41

假设插件为 input 分配了一个 label ,并且具有 error 类,那么您可以这样做:

$('label.error').prev('input').val('');

更准确一点,因为验证插件还为错误标签分配了一个 for 属性:

$(document).ready(
  function(){

     // call to the plug-in here, input-clearing afterwards
     // (though I'm not sure it matters, since it's dependant
     // on CSS selectors to work, which aren't dependant on
     // the plug-in itself).

     $('label.error').each(
       function(){
         var errorInputID = $(this).attr('for');
         $('#'+errorInputID).val('');
       });
  });

我假设“验证插件”指的是这个,位于:http://bassistance.de/jquery-plugins/jquery-plugin-validation/,如果我错了那么这个,显然,可能行不通。

Assuming that the plugin assigns a label for the input with a class of error, then you could do this:

$('label.error').prev('input').val('');

To be a little more accurate, since the validation plugin also assigns a for attribute to the error labels:

$(document).ready(
  function(){

     // call to the plug-in here, input-clearing afterwards
     // (though I'm not sure it matters, since it's dependant
     // on CSS selectors to work, which aren't dependant on
     // the plug-in itself).

     $('label.error').each(
       function(){
         var errorInputID = $(this).attr('for');
         $('#'+errorInputID).val('');
       });
  });

I'm assuming, by 'validation plugin' that you mean this one, at: http://bassistance.de/jquery-plugins/jquery-plugin-validation/, if I'm wrong then this, obviously, might not work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文