18910140161

JavaScript-如何在输入中显示表单验证错误,而不是在弹出窗口中一个接一个地显示?-堆栈溢出

顺晟科技

2022-10-19 14:27:35

51

这里有一个非常简单的表单,需要2个输入。

当您点击提交按钮而不填写它们时-会弹出窗口说您应该这样做。问题是弹出窗口是一个接一个地显示的-例如,如果两个输入都没有填满,只有第一个输入才会有这个弹出窗口。当第一个被填满时,它会转到第二个,反之亦然。

是否有任何方法可以同时显示验证过程中未填充/填充不正确的所有字段?因此用户立即看到他/她必须填写的所有内容?

我对此相当陌生,所以请帮我找到纯JS中的解决方案(如果是关于JS的)。 下面是代码:

<html lang="eng">

<head>
    <title>Title</title>
    <meta content="width=device-width, initial-scale=1" name="viewport" />
</head>

<body>
    <form id="mainForm" action="#" method="POST">
        <div>
            <div>
                <label for="first_name" title="first_name">First name<span class="mandatory">*</span></label>
                <input id="first_name" name="first_name" type="text" value="" required=""
                    oninvalid="setCustomValidity('Enter first name')" oninput="setCustomValidity('')"
                    placeholder="Enter first name">
                <p class="error_message"></p>
            </div>
            <div>

                <label for="lastName" title="lastName">Last name<span class="mandatory">*</span></label>
                <input id="lastName" name="lastName" type="text" value="" required=""
                    oninvalid="setCustomValidity('Enter last name')" oninput="setCustomValidity('')"
                    placeholder="Enter last name">
                <p class="error_message"></p>
            </div>

            <div class="">
                <input class="email_btn btn btn-block" type="submit" value="Submit">
            </div>
        </div>
    </form>
</body>

</html>

顺晟科技:

您提供的代码使用了JavaScript的内置函数setCustomValide()。这很可能是弹出的原因。相反,我们可以编写一个自定义函数来显示一个带有文本的小段落/span。

这里有一个HTML表单,但是在单击提交按钮时调用了自定义函数:

<html lang="eng">

<head>
    <title>Title</title>
    <meta content="width=device-width, initial-scale=1" name="viewport" />
</head>

<body>
    <form id="mainForm" action="#" method="POST">
        <div>
            <div>
                <label for="first_name" title="first_name">First name<span class="mandatory">*</span></label>
                <input id="first_name" name="first_name" type="text" value="" required=""
                    oninvalid="setCustomValidity('Enter first name')" oninput="setCustomValidity('')"
                    placeholder="Enter first name">
                <p class="error_message"></p>
            </div>
            <div>

                <label for="lastName" title="lastName">Last name<span class="mandatory">*</span></label>
                <input id="lastName" name="lastName" type="text" value="" required=""
                    oninvalid="setCustomValidity('Enter last name')" oninput="setCustomValidity('')"
                    placeholder="Enter last name">
                <p class="error_message"></p>
            </div>

            <div class="">
                <input class="email_btn btn btn-block" type="submit" value="Submit">
            </div>
        </div>
    </form>
</body>

</html>

使其发生的JS:

(对输入为空做出反应并让用户知道哪些字段需要修复的自定义函数,将代码放在html-page中的标记之前)

<html lang="eng">

<head>
    <title>Title</title>
    <meta content="width=device-width, initial-scale=1" name="viewport" />
</head>

<body>
    <form id="mainForm" action="#" method="POST">
        <div>
            <div>
                <label for="first_name" title="first_name">First name<span class="mandatory">*</span></label>
                <input id="first_name" name="first_name" type="text" value="" required=""
                    oninvalid="setCustomValidity('Enter first name')" oninput="setCustomValidity('')"
                    placeholder="Enter first name">
                <p class="error_message"></p>
            </div>
            <div>

                <label for="lastName" title="lastName">Last name<span class="mandatory">*</span></label>
                <input id="lastName" name="lastName" type="text" value="" required=""
                    oninvalid="setCustomValidity('Enter last name')" oninput="setCustomValidity('')"
                    placeholder="Enter last name">
                <p class="error_message"></p>
            </div>

            <div class="">
                <input class="email_btn btn btn-block" type="submit" value="Submit">
            </div>
        </div>
    </form>
</body>

</html>

最后,下面是您自己的代码

<html lang="eng">

<head>
    <title>Title</title>
    <meta content="width=device-width, initial-scale=1" name="viewport" />
</head>

<body>
    <form id="mainForm" action="#" method="POST">
        <div>
            <div>
                <label for="first_name" title="first_name">First name<span class="mandatory">*</span></label>
                <input id="first_name" name="first_name" type="text" value="" required=""
                    oninvalid="setCustomValidity('Enter first name')" oninput="setCustomValidity('')"
                    placeholder="Enter first name">
                <p class="error_message"></p>
            </div>
            <div>

                <label for="lastName" title="lastName">Last name<span class="mandatory">*</span></label>
                <input id="lastName" name="lastName" type="text" value="" required=""
                    oninvalid="setCustomValidity('Enter last name')" oninput="setCustomValidity('')"
                    placeholder="Enter last name">
                <p class="error_message"></p>
            </div>

            <div class="">
                <input class="email_btn btn btn-block" type="submit" value="Submit">
            </div>
        </div>
    </form>
</body>

</html>

这是使用自定义脚本来获得一个您可以自己设置样式和增强的框,在本例中位于表单下面。但是,如果您接受一些默认设置(由于浏览器的不同,可能不是统一的样式),您也可以删除原始代码中的JavaScript函数。这将给您留下一个使用已经存在属性的通用消息,该属性产生以下内容:

要实现这种行为,请将每个字段的标记改为如下所示:

<html lang="eng">

<head>
    <title>Title</title>
    <meta content="width=device-width, initial-scale=1" name="viewport" />
</head>

<body>
    <form id="mainForm" action="#" method="POST">
        <div>
            <div>
                <label for="first_name" title="first_name">First name<span class="mandatory">*</span></label>
                <input id="first_name" name="first_name" type="text" value="" required=""
                    oninvalid="setCustomValidity('Enter first name')" oninput="setCustomValidity('')"
                    placeholder="Enter first name">
                <p class="error_message"></p>
            </div>
            <div>

                <label for="lastName" title="lastName">Last name<span class="mandatory">*</span></label>
                <input id="lastName" name="lastName" type="text" value="" required=""
                    oninvalid="setCustomValidity('Enter last name')" oninput="setCustomValidity('')"
                    placeholder="Enter last name">
                <p class="error_message"></p>
            </div>

            <div class="">
                <input class="email_btn btn btn-block" type="submit" value="Submit">
            </div>
        </div>
    </form>
</body>

</html>
  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航