在同一页面上验证两个表单(Validation of two forms on same page)

编程入门 行业动态 更新时间:2024-10-22 10:38:22
在同一页面上验证两个表单(Validation of two forms on same page)

我在同一个网页上有两个表单,但我的验证脚本不起作用,即使所有输入名称都不同,表单ID也不同。

小提琴

$(document).ready(function () {
    // Place ID's of all required fields here.
    required = ["namebud", "tlfbud", "budbud"];
    // If using an ID other than #email or #error then replace it here
    email = $("#emailbud");
    errornotice = $("#error");
    // The text to show up within a field when it is incorrect
    emptyerror = "Please write";
    emailerror = "Please write email";

    $("#formbud").submit(function () {
        //Validate required fields
        for (i = 0; i < required.length; i++) {
            var input = $('#' + required[i]);
            if ((input.val() === "") || (input.val() == emptyerror)) {
                input.addClass("needsfilled");
                input.val(emptyerror);
                errornotice.fadeIn(750);
            } else {
                input.removeClass("needsfilled");
            }
        }
        // Validate the e-mail.
        if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
            email.addClass("needsfilled");
            email.val(emailerror);
        }

        //if any inputs on the page have the class 'needsfilled' the form will not submit
        if ($(":input").hasClass("needsfilled")) {
            return false;
        } else {
            errornotice.hide();
            $(this).hide();
            $(".result").show();
            return true;
        }
    });
    // Clears any fields in the form when the user clicks on them
    $(":input").focus(function () {
        if ($(this).hasClass("needsfilled")) {
            $(this).val("");
            $(this).removeClass("needsfilled");
        }
    });
});


$(document).ready(function () {
    // Place ID's of all required fields here.
    required = ["messageride", "nameride", "tlfride"];
    // If using an ID other than #email or #error then replace it here
    email = $("#emailride");
    errornotice = $("#error");
    // The text to show up within a field when it is incorrect
     emptyerror = "Please write";
    emailerror = "Please write email";

    $("#formride").submit(function () {
        //Validate required fields
        for (i = 0; i < required.length; i++) {
            var input = $('#' + required[i]);
            if ((input.val() === "") || (input.val() == emptyerror)) {
                input.addClass("needsfilled");
                input.val(emptyerror);
                errornotice.fadeIn(750);
            } else {
                input.removeClass("needsfilled");
            }
        }
        // Validate the e-mail.
        if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
            email.addClass("needsfilled");
            email.val(emailerror);
        }

        //if any inputs on the page have the class 'needsfilled' the form will not submit
        if ($(":input").hasClass("needsfilled")) {
            return false;
        } else {
            errornotice.hide();
            $(this).hide();
            $(".result").show();
            return true;
        }
    });

    // Clears any fields in the form when the user clicks on them
    $(":input").focus(function () {
        if ($(this).hasClass("needsfilled")) {
            $(this).val("");
            $(this).removeClass("needsfilled");
        }
    });
}); 
  
input.needsfilled, textarea.needsfilled {
    color:red;
} 
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<form method="post" action="/mailboffer" id="formbud">
    <input type="text" name="namebud" id="namebud" placeholder="name" value="" />
    <input type="text" name="emailbud" id="emailbud" placeholder="mail" value="" />
    <input type="text" name="tlfbud" id="tlfbud" placeholder="tel" value="" />
    <input type="text" name="bud" id="budbud" placeholder="offer" value="" />
    <textarea name="messagebud" placeholder="comment"></textarea>
    <input type="submit" class="button darkblue bud" name="submit" value="Send" />
</form>
<br/>
<form method="post" action="/mailride" id="formride">
    <input type="text" name="nameride" id="nameride" placeholder="name" value="" />
    <input type="text" name="emailride" id="emailride" placeholder="mail" value="" />
    <input type="text" name="tlfride" id="tlfride" placeholder="trl" value="" />
    <textarea name="messageride" id="messageride" placeholder="message"></textarea>
    <input type="submit" class="button darkblue ride" name="submit" value="Send" />
</form> 
  
 

希望有人可以帮助我。

I have two forms on the same webpage, but my validation script dosen't work, even all input names are different and the form id is also different.

Fiddle

$(document).ready(function () {
    // Place ID's of all required fields here.
    required = ["namebud", "tlfbud", "budbud"];
    // If using an ID other than #email or #error then replace it here
    email = $("#emailbud");
    errornotice = $("#error");
    // The text to show up within a field when it is incorrect
    emptyerror = "Please write";
    emailerror = "Please write email";

    $("#formbud").submit(function () {
        //Validate required fields
        for (i = 0; i < required.length; i++) {
            var input = $('#' + required[i]);
            if ((input.val() === "") || (input.val() == emptyerror)) {
                input.addClass("needsfilled");
                input.val(emptyerror);
                errornotice.fadeIn(750);
            } else {
                input.removeClass("needsfilled");
            }
        }
        // Validate the e-mail.
        if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
            email.addClass("needsfilled");
            email.val(emailerror);
        }

        //if any inputs on the page have the class 'needsfilled' the form will not submit
        if ($(":input").hasClass("needsfilled")) {
            return false;
        } else {
            errornotice.hide();
            $(this).hide();
            $(".result").show();
            return true;
        }
    });
    // Clears any fields in the form when the user clicks on them
    $(":input").focus(function () {
        if ($(this).hasClass("needsfilled")) {
            $(this).val("");
            $(this).removeClass("needsfilled");
        }
    });
});


$(document).ready(function () {
    // Place ID's of all required fields here.
    required = ["messageride", "nameride", "tlfride"];
    // If using an ID other than #email or #error then replace it here
    email = $("#emailride");
    errornotice = $("#error");
    // The text to show up within a field when it is incorrect
     emptyerror = "Please write";
    emailerror = "Please write email";

    $("#formride").submit(function () {
        //Validate required fields
        for (i = 0; i < required.length; i++) {
            var input = $('#' + required[i]);
            if ((input.val() === "") || (input.val() == emptyerror)) {
                input.addClass("needsfilled");
                input.val(emptyerror);
                errornotice.fadeIn(750);
            } else {
                input.removeClass("needsfilled");
            }
        }
        // Validate the e-mail.
        if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
            email.addClass("needsfilled");
            email.val(emailerror);
        }

        //if any inputs on the page have the class 'needsfilled' the form will not submit
        if ($(":input").hasClass("needsfilled")) {
            return false;
        } else {
            errornotice.hide();
            $(this).hide();
            $(".result").show();
            return true;
        }
    });

    // Clears any fields in the form when the user clicks on them
    $(":input").focus(function () {
        if ($(this).hasClass("needsfilled")) {
            $(this).val("");
            $(this).removeClass("needsfilled");
        }
    });
}); 
  
input.needsfilled, textarea.needsfilled {
    color:red;
} 
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<form method="post" action="/mailboffer" id="formbud">
    <input type="text" name="namebud" id="namebud" placeholder="name" value="" />
    <input type="text" name="emailbud" id="emailbud" placeholder="mail" value="" />
    <input type="text" name="tlfbud" id="tlfbud" placeholder="tel" value="" />
    <input type="text" name="bud" id="budbud" placeholder="offer" value="" />
    <textarea name="messagebud" placeholder="comment"></textarea>
    <input type="submit" class="button darkblue bud" name="submit" value="Send" />
</form>
<br/>
<form method="post" action="/mailride" id="formride">
    <input type="text" name="nameride" id="nameride" placeholder="name" value="" />
    <input type="text" name="emailride" id="emailride" placeholder="mail" value="" />
    <input type="text" name="tlfride" id="tlfride" placeholder="trl" value="" />
    <textarea name="messageride" id="messageride" placeholder="message"></textarea>
    <input type="submit" class="button darkblue ride" name="submit" value="Send" />
</form> 
  
 

Hope somebody can help me out.

最满意答案

选择器“:input”将作用于所有输入字段,而不管包含的形式如何。 我建议你在表单id之前添加你的选择器,就像这个$(“#formride:input”)这将确保验证和css类应用于正确的元素

The selector ":input" will act on all input fields, regardless of the containing form. I would suggest you prepend your selector with the form id as well like this $("#formride :input") This will ensure that the validation and css classes are applied to the correct elements

更多推荐

本文发布于:2023-08-05 09:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1431197.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   两个   页面   在同一   page

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!