简体   繁体   English

使用 Javascript 的电子邮件验证不适用于 HTML

[英]Email validation using Javascript doesn't work with HTML

I'm trying to let user check if his email adress pass RFC standard, comparing it with regular expression dropped below.我试图让用户检查他的电子邮件地址是否通过 RFC 标准,并将其与下面的正则表达式进行比较。

I already checked few examples on console using function responsible for excecute validation test.我已经使用负责执行验证测试的函数在控制台上检查了几个示例。 I'm aware of possibilities that my regex is not ready for, but whenever I tried a few examples on console - it works.我知道我的正则表达式还没有准备好的可能性,但是每当我在控制台上尝试一些示例时 - 它都有效。

Problem begins when function gets value from included 'text' HTML form.当函数从包含的“文本”HTML 表单中获取值时,问题就开始了。

Using Javascript I also checked how value is interpretated by browser (Simply pasting text into another div) - result is ok.使用 Javascript 我还检查了浏览器如何解释值(只需将文本粘贴到另一个 div 中) - 结果没问题。

Why validation doesn't work properly whenever use input value on HTML forms?为什么在 HTML 表单上使用输入值时验证无法正常工作?

 const regular = /^[az/d]+[/d/w.-]*@(?:[az/d-]+[az/d-]\\.+){1,5}[az]{1,3}[az]$/i; test = function(email) { if (regular.test(email) == false) { alert('Your email adress is not correct. Try again!') document.getElementById('result').innerHTML = document.getElementById('text').value; return false; } else { alert('Match'); return true; } }
 <div id='form-structure'> <form name='form1' action='#'> <label for='email' name='email'>E-mail</label> <input type='text' name='text' id='text' placeholder="Enter your email" required> <button type='submit' name='submit' onclick="test(document.form1.text)" value='Submit'>Test</button> </form> </div> <div id='result' style='font-size: 15px'> </div>

You are not passing the correct value to your function.您没有将正确的值传递给您的函数。 In your function, the email variable is在您的函数中, email变量是

<input type="text" name="text" id="text" placeholder="Enter your email" required></input>

What you need to pass to your function is the actual value of the element #test .您需要传递给函数的是元素#test的实际值。 This can be retreive using this selector.这可以使用这个选择器来检索。 You can use this selector inside the function, or directly into the function call.你可以在函数内部使用这个选择器,或者直接进入函数调用。 The latter is prefered, like Mr Zach pointed out.正如扎克先生指出的那样,后者是首选。 :

 document.getElementById('text').value;

Also, you regex does not seams right.此外,您的正则表达式不正确。 You are using foward slash ( / ) in your digit token and your word token.您在数字标记和单词标记中使用正斜杠 ( / )。 You need backslash ( \\ ) in that case.在这种情况下,您需要反斜杠 ( \\ )。

I've also removed the submit behaviour of your form, this is to facilitate debugging.我还删除了表单的submit行为,这是为了方便调试。 It was not the cause of your problem.这不是你的问题的原因。

 const regular = /^[az\\d]+[\\d\\w.-]*@(?:[az\\d-]+[az\\d-]\\.+){1,5}[az]{1,3}[az]$/i; var test = function(email) { if (regular.exec(email) == null) { alert('Your email adress is not correct. Try again!') document.getElementById('result').innerHTML = document.getElementById('text').value; return false; } else { alert('Match'); return true; } }
 <div id='form-structure'> <form name='form1' action=''> <label for='email' name='email'>E-mail</label> <input type='text' name='text' id='text' placeholder="Enter your email" required> <button type='button' name='submit' onclick="test(document.getElementById('text').value)" value='Submit'>Test</button> </form> </div> <div id='result' style='font-size: 15px'> </div>

While this may not be the best of code example, the simple solution to your issue lies in document.form1.text .虽然这可能不是最好的代码示例,但解决您问题的简单方法在于document.form1.text Simple change it to document.forms.form1.text .简单地将其更改为document.forms.form1.text

Eg例如

<button type='submit' name='submit' onclick="test(document.forms.form1.text.value)" 
         value='Submit'>Test</button>

EDIT:编辑:

I missed the .value before.我之前错过了 .value 。 Now it should work.现在它应该可以工作了。

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

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