开发者

one error message for multiple fields

开发者 https://www.devze.com 2023-03-15 05:12 出处:网络
How can I display one error message after validating multiple fie开发者_JAVA技巧lds? For example if i have 3 grouped text fields and i would like to show an error message ONLY after validating all t

How can I display one error message after validating multiple fie开发者_JAVA技巧lds?

For example if i have 3 grouped text fields and i would like to show an error message ONLY after validating all three fields.


Check out the jQuery validation plugin, especially the parts around groups:

http://docs.jquery.com/Plugins/Validation/validate#toptions


With no code example of the form, I'm not sure what you are validating against or if each field had a unique validation, so I had to guess:

jsfiddle: http://jsfiddle.net/jensbits/vVe3r/3/

<form id="myform">
<input type="text" /><br />
<input type="text" /><br />
<input type="text" /><br />
<input type="submit" value="Submit" />

$(function() {
    $("#myform").submit(function(e) {
        e.preventDefault();
        var validfields = true;
        $("input").each(function() {
            if ($(this).val() === "") {
                validfields = false;
            }
            if (!validfields) {
                $("#error").html("<span style='color:red'>Error in form</span>");
            }else{
                $("#error").html(""); //if submit posts back to same page
                $("#myForm").submit();
            }
    });
});
    $("input").blur(function() {
        if ($(this).val() === "") {
            $("#error").html("<span style='color:red'>Error in form</span>");
        }else{
            $("#error").html("");
        }
    }); 


});
0

精彩评论

暂无评论...
验证码 换一张
取 消