Javascript 如何使用html表单验证来显示图像

Javascript 如何使用html表单验证来显示图像,javascript,html,validation,forms,Javascript,Html,Validation,Forms,我创建了一个HTML表单,其中有许多不同的文本框。我想做的是创建一个javascript脚本,在没有文本的框旁边显示小图像。下面是我想做的一个例子: 目前,我已经创建了下面的脚本来验证我想要的表单,但是它只显示一个弹出框,因为我不知道如何显示图像/文本。以下是我的代码: <script type="text/javascript"> function validateForm() { var x=document.forms["email_f

我创建了一个HTML表单,其中有许多不同的文本框。我想做的是创建一个javascript脚本,在没有文本的框旁边显示小图像。下面是我想做的一个例子:

目前,我已经创建了下面的脚本来验证我想要的表单,但是它只显示一个弹出框,因为我不知道如何显示图像/文本。以下是我的代码:

    <script type="text/javascript">
    function validateForm()
    {
        var x=document.forms["email_form"]["name"].value;
        if (x==null || x=="")
        {
            alert("Please enter your name in the box provided.");
            return false;
        }

        var x=document.forms["email_form"]["email"].value;
        if (x==null || x=="")
        {
            alert("Please enter your e-mail address in the box provided.");
            return false;
        }

        var x=document.forms["message-title"]["name"].value;
        if (x==null || x=="")
        {
            alert("Please enter a message title in the box provided.");
            return false;
        }

        var x=document.forms["email_form"]["message"].value;
        if (x==null || x=="")
        {
            alert("Please enter your message in the box provided.");
            return false;
        }
    }
    </script>

函数validateForm()
{
var x=document.forms[“email_form”][“name”]。值;
如果(x==null | | x==“”)
{
警告(“请在提供的框中输入您的姓名。”);
返回false;
}
var x=document.forms[“email_form”][“email”]。值;
如果(x==null | | x==“”)
{
警告(“请在提供的框中输入您的电子邮件地址。”);
返回false;
}
var x=document.forms[“message title”][“name”]。值;
如果(x==null | | x==“”)
{
警报(“请在提供的框中输入消息标题”);
返回false;
}
var x=document.forms[“email_form”][“message”]。值;
如果(x==null | | x==“”)
{
警告(“请在提供的框中输入您的消息。”);
返回false;
}
}

请有人给我指出正确的方向吗?

您可以在输入元素旁边设置错误图像元素,但将显示属性隐藏在css中。可以为链接到其输入元素的每个错误图像设置id。例如,对于电子邮件输入,除此之外还有和img元素。有这样的初始css->

#email_form img{
    display: none;
}
然后在javascript中,只需显示隐藏的图像-

    var x=document.forms["email_form"]["email"].value;
    if (x==null || x=="")
    {
        var error_image = document.getElementById('error_email');
        error_image.style.display = 'inline';
        alert("Please enter your e-mail address in the box provided.");
        return false;
    }

在css中定义一个类

.validateimage
{
 display:none;
}
并将该类分配给所有图像标记,如

<img class="validateimage" id="image1">
.
.
.

.
.
.
然后


函数validateForm()
{
var x=document.forms[“email_form”][“name”]。值;
如果(x==null | | x==“”)
{
document.getElementById('image1').style.display='block';
返回false;
}
var x=document.forms[“email_form”][“email”]。值;
如果(x==null | | x==“”)
{
document.getElementById('image2').style.display='block';
返回false;
}
var x=document.forms[“message title”][“name”]。值;
如果(x==null | | x==“”)
{
document.getElementById('image3').style.display='block';
返回false;
}
var x=document.forms[“email_form”][“message”]。值;
如果(x==null | | x==“”)
{
document.getElementById('image4').style.display='block';
返回false;
}
}
 <script type="text/javascript">
        function validateForm()
        {
            var x=document.forms["email_form"]["name"].value;
            if (x==null || x=="")
            {
                document.getElementById('image1').style.display='block';
                return false;
            }

            var x=document.forms["email_form"]["email"].value;
            if (x==null || x=="")
            {
                document.getElementById('image2').style.display='block';

                return false;
            }

            var x=document.forms["message-title"]["name"].value;
            if (x==null || x=="")
            {
                document.getElementById('image3').style.display='block';
                return false;
            }

            var x=document.forms["email_form"]["message"].value;
            if (x==null || x=="")
            {
                document.getElementById('image4').style.display='block';
                return false;
            }
        }
        </script>