简体   繁体   English

Javascript消息警报框

[英]Javascript message alert box

I have this javascript code: 我有这个javascript代码:

function validate()
{

    var email=document.getElementById('email').value;


    var emailRegex=/^[a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,6}$/;
    var emailResult=emailRegex.test(email);
    alert("email:" +emailResult);
}

and the html part, on a form tag: 和html部分,在表单标签上:

<input type="text" id="email" placeholder="EMAIL"><br><br>
<input type="submit" value="REGISTER" id="butt">

I want to print a message everytime that the test of Regex returns false,but I don't want to use an alert box. 我想每次Regex测试返回false时都打印一条消息,但是我不想使用警报框。 How can I do this? 我怎样才能做到这一点?

Form : 形式:

         <form id="registration" onsubmit="validate()">
                <h3>S'ENREGISTRER</h3>
                <label for="button">FULL NAME: </label>
                <small>*</small>
                <input type="text" name="fname" id="fullname" placeholder="FULL NAME"><br><br>
                <label for="button">LAST NAME:</label>
                <small>*</small>
                <input type="text" name="lname" id="lastname" placeholder="LAST NAME"><br><br>
                <label for="button">USERNAME:</label>
                <small>*</small>
                <input type="text" name="uname" id="username" placeholder="USERNAME"><br><br>
                <label for="button">PASSWORD:</label>
                <small>*</small>
                <input type="password" name="pass" id="password" placeholder="PASSWORD"><br><br>
                <label id = "kjo" for="button">EMAIL:</label>
                <small>*</small>
                <input type="text" id="email" placeholder="EMAIL"><br><br>
                <input type="submit" value="REGISTER" id="butt" onclick="validate()">
                <p id="result"></p>
                <br><br>
                <a href="login.html">LOGIN</a>
            </form>

One possibility would be to add a p tag (or div , span and so on), which serves as a "field" to show the result of the test. 一种可能性是添加一个p标签(或divspan等),该标签用作显示测试结果的“字段”。

In this case, I'm using a p tag. 在这种情况下,我使用的是p标签。 To print the result, I use innerText like this: 为了打印结果,我使用innerText像这样:

 function validate() { var email = document.getElementById('email').value; var emailRegex = /^[a-z0-9._-]+@[a-z0-9._-]+\\.[az]{2,6}$/; var emailResult = emailRegex.test(email); //alert("email:" + emailResult); // Show result withing the added p tag document.getElementById('result').innerText = "email: " + emailResult; } 
 <input type="text" id="email" placeholder="EMAIL"><br><br> <input type="submit" value="REGISTER" id="butt" onclick="validate()"> <p id="result"></p> 

PS: I also added the onclick attribute to your button, to fire the function when the button get's clicked. PS:我还向您的按钮添加了onclick属性,以在单击按钮时触发该功能。


Edit: 编辑:

If you want to display the error message, after the form gets submitted, you might want to take a look at PHP . 如果要显示错误消息,请提交表单显示PHP With JavaScript on the client side (what we are doing here), you can't achieve that. 在客户端使用JavaScript(我们在这里所做的事情),您将无法实现。

One way to get around this problem, would be to prevent the form from submitting if the email is invalid: 解决此问题的一种方法是,如果电子邮件无效,则阻止表单提交:

 function validate(event) { // Mind the "event" parameter here var email = document.getElementById('email').value; var emailRegex = /^[a-z0-9._-]+@[a-z0-9._-]+\\.[az]{2,6}$/; var emailResult = emailRegex.test(email); //alert("email:" + emailResult); // Check if the email is invalid. If not, dont' submit the form if (emailResult == false) { event.preventDefault(); // This line prevents the form from submitting document.getElementById('result').innerText = "email: " + emailResult; } } 
 <form id="registration" onsubmit="validate(event)"> <!-- mind the "event" parameter here --> <h3>S'ENREGISTRER</h3> <label for="button">FULL NAME: </label> <small>*</small> <input type="text" name="fname" id="fullname" placeholder="FULL NAME"><br><br> <label for="button">LAST NAME:</label> <small>*</small> <input type="text" name="lname" id="lastname" placeholder="LAST NAME"><br><br> <label for="button">USERNAME:</label> <small>*</small> <input type="text" name="uname" id="username" placeholder="USERNAME"><br><br> <label for="button">PASSWORD:</label> <small>*</small> <input type="password" name="pass" id="password" placeholder="PASSWORD"><br><br> <label id="kjo" for="button">EMAIL:</label> <small>*</small> <input type="text" id="email" placeholder="EMAIL"><br><br> <input type="submit" value="REGISTER" id="butt"> <!-- I removed the onclick attribute here because it's already in the form tag --> <p id="result"></p> <br><br> <a href="login.html">LOGIN</a> </form> <p id="result"></p> 

As the previous answer says, you can insert a tag to show the result, after each field you want to validate. 如上一个答案所述,您可以在要验证的每个字段之后插入一个标签以显示结果。

For email as you mentioned in question, I write the part of showing error message as below. 对于您所提到的电子邮件,我编写了显示错误消息的部分,如下所示。

 Element.prototype.remove = function() { this.parentElement.removeChild(this); }; NodeList.prototype.remove = HTMLCollection.prototype.remove = function() { for (var i = this.length - 1; i >= 0; i--) { if (this[i] && this[i].parentElement) { this[i].parentElement.removeChild(this[i]); } } }; function insertAfter(newNode, referenceNode) { referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling); } function validate(event) { var emailInput = document.getElementById("email"); var email = emailInput.value; var emailRegex = /^[a-z0-9._-]+@[a-z0-9._-]+\\.[az]{2,6}$/; var emailResult = emailRegex.test(email); if (!emailResult) { var errorDiv = document.getElementById("error"); if (!errorDiv) { var div = document.createElement("div"); div.innerHTML = "Insert a valid Email"; div.classList.add("error"); div.id = "error"; insertAfter(div, emailInput); emailInput.classList.add("error-input"); } event.preventDefault(); } else { var errorDiv = document.getElementById("error"); if (errorDiv) { errorDiv.remove(); emailInput.classList.remove("error-input"); } } } 
 .error { color: red; } .error-input { border: 1px solid red; border-radius: 5px; padding: 5px } 
 <form id="registration" onsubmit="validate()"> <h3>S'ENREGISTRER</h3> <label for="button">FULL NAME: </label> <small>*</small> <input type="text" name="fname" id="fullname" placeholder="FULL NAME"><br><br> <label for="button">LAST NAME:</label> <small>*</small> <input type="text" name="lname" id="lastname" placeholder="LAST NAME"><br><br> <label for="button">USERNAME:</label> <small>*</small> <input type="text" name="uname" id="username" placeholder="USERNAME"><br><br> <label for="button">PASSWORD:</label> <small>*</small> <input type="password" name="pass" id="password" placeholder="PASSWORD"><br><br> <label id="kjo" for="button">EMAIL:</label> <small>*</small> <input type="text" id="email" placeholder="EMAIL"><br><br> <input type="submit" value="REGISTER" id="butt" onclick="validate(event)"> <p id="result"></p> <br><br> <a href="login.html">LOGIN</a> </form> 

You can also write an event listener onchange or onmouseout or onfocusout for your fields for validating them immediately after each change. 您也可以为字段编写事件侦听器onchangeonmouseoutonfocusout ,以便在每次更改后立即对其进行验证。

Hope this helps. 希望这可以帮助。

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

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