简体   繁体   English

表单验证,无警报

[英]Form Validation without Alerts

I was wondering if there's a way to validate the form without using alerts. 我想知道是否有一种无需使用警报即可验证表单的方法。 Like usually you would see red text beside the input box if you typed in the wrong information or something. 通常,如果您输入了错误的信息或其他内容,您将在输入框旁边看到红色文本。 And I don't want to use Jquery. 而且我不想使用Jquery。 I've included a div for the red text messages in the html - namemsg, commentmsg, emailmsg. 我在html中包括了红色文本消息的div-namemsg,commentmsg,emailmsg。

So far I've only got the code with alerts. 到目前为止,我只收到带有警报的代码。 JavaScript: JavaScript:

function validateUser()
{
    var x=document.forms["myForm"]["email"].value;
    var atpos=x.indexOf("@");
    var dotpos=x.lastIndexOf(".");
    if (atpos<2 || dotpos<atpos+2 || dotpos+2>=x.length)
    {
        alert("Not a valid e-mail address");
        return false;
    }

    {
        alert("Valid Input");
    }

    return true;
}

Html HTML

 <form name="myForm" method="post">
    <label>*Name:</label>
    <input type="text" name="name" title="Enter a your name" placeholder="Your Name" onclick="select()" required/>
    <div id="namemsg"></div><br/>

    <label>*E-mail:</label>
    <input type="text" name="email" title="Enter a valid email address" placeholder="me@example.com" onclick="select()" required/>
    <div id="emailmsg">  </div><br/>

    <label>*Comment:</label>
    <textarea name="comment" title="Enter your comments" placeholder="Enter your comments." onclick="select()" required/></textarea>
    <div id="commentmsg">  </div>
    <label id="error"> </label> <br/>
    <input type="submit" value="Submit" onclick="return validateUser()"> 
</form>

You can place a span/label next to validated field with predefined text and style and display style of none . 您可以在已验证字段旁边放置跨度/标签,其中预定义的文本和样式以及显示样式均为none If validation detects an invalid input - change the display style to "" to make the label visible. 如果验证检测到无效输入-将显示样式更改为“”以使标签可见。

Update 更新资料

I do see you have already predefined DIV. 我确实看到您已经预定义了DIV。 Define it as 定义为

<div id="emailmsg" style="color:Red;display:none">Not a valid e-mail address</div>

And in JavaScript instead of 并且用JavaScript代替

alert("Not a valid e-mail address");

Use 采用

document.getElementById("emailmsg").style.display=""

Instead of showing alert message box you can color that textbox borders to red color and show the error beneath it. 除了显示警报消息框外,您还可以将文本框的边框颜色涂成红色,并在其下方显示错误。 Like, if you replace the following line of code: 就像,如果您替换下面的代码行:

alert("Not a valid e-mail address");

With: 带有:

document.forms["myForm"]["email"].style.border = "1px solid red";
document.getElementById("emailmsg").innerHTML = "Not a valid e-mail address";

The above code will highlight the borders of email field with red color and show error in emailmsg div. 上面的代码将用红色突出显示电子邮件字段的边框,并在emailmsg div中显示错误。

Hope this can help. 希望这会有所帮助。

your html: 您的html:

    <form name="myForm" method="post">
        <label>*Name:</label>
        <input type="text" name="name" title="Enter a your name" placeholder="Your Name" onclick="select()" required/>
        <div id="namemsg"></div><br/>

        <label>*E-mail:</label>
        <input type="text" name="email" title="Enter a valid email address" placeholder="me@example.com" onclick="select()" required/>
// this span is hidden until you show it as error msg
    <span id="error" style='display: none'>Not a valid e-mail address<span>
        <div id="emailmsg">  </div><br/>

        <label>*Comment:</label>
        <textarea name="comment" title="Enter your comments" placeholder="Enter your comments." onclick="select()" required/></textarea>
        <div id="commentmsg">  </div>
        <label id="error"> </label> <br/>
        <input type="submit" value="Submit" onclick="return validateUser()"> 
    </form>

your js: 您的js:

    function validateUser()
    {{
    var x=document.forms["myForm"]["email"].value;
    var atpos=x.indexOf("@");
    var dotpos=x.lastIndexOf(".");
    if (atpos<2 || dotpos<atpos+2 || dotpos+2>=x.length)
    {
//instead of alerting error show your error span try to put nice css for it
    document.getElementById('error').style.display="block";
    return false;
    }

    {alert("Valid Input");}
    return true;
        }

First, I would recommend attaching the execute of the function to the event OnSubmit . 首先,我建议将函数的执行附加到事件OnSubmit

HTML: HTML:

<form method="post" name="myForm" onsubmit="return validateUser(this);">

JS: JS:

function validateUser(formObj) {}

Next, the only thing you need to do, is just have some sort of container, and if the function detects an error, just append the error text to it. 接下来,您唯一需要做的就是拥有某种容器,并且如果函数检测到错误,只需将错误文本附加到该容器中即可。

At the start of each function call, empty the container. 在每个函数调用的开始,清空容器。

If you want the error to appear next to the textbox/any input tag, just add a div / span next to each field and append the error to it. 如果您希望错误显示在文本框/任何输入标签的旁边,只需在每个字段旁边添加一个div / span并将错误附加到它。

document.getElementById("emailmsg").innerHTML = "Not cool...";

This should help. 这应该有所帮助。

if (atpos<2 || dotpos<atpos+2 || dotpos+2>=x.length){
// validate message function call
valMessage('emailmsg', 'Not a valid e-mail address');
return false;
}

// validate message function 
function valMessage(divName, content) {
     document.getElementById(divName).innerHTML = content;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM