javascript 如果电子邮件输入字段有效,如何仅使用 onClick?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15553443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use onClick ONLY if email input field is valid?
提问by Demuri Celidze
Here's code:
这是代码:
<input id="subscribe-email" type="email" placeholder="[email protected]" />
<button type="submit" id="subscribe-submit" onClick="javascript:omgemailgone();" />
How do I check and run JS function only if email is valid (validation by user-agent, no additional validations)?
仅当电子邮件有效时(由用户代理验证,无需额外验证),我如何检查和运行 JS 函数?
UPDATE. New browsers can validate input=email by themselves, also there are pseudo classes :valid and :invalid. I need to run function only if browser 'knows' that email is valid.
更新。新的浏览器可以自己验证 input=email,也有伪类 :valid 和 :invalid。仅当浏览器“知道”该电子邮件有效时,我才需要运行函数。
回答by danii
You can use the .checkValidity()
method of the input element(in this case, the email input field). This will return a boolean indicating wether the input is valid or not.
Here is a fiddle to play with:
您可以使用input 元素的.checkValidity()
方法(在本例中为电子邮件输入字段)。这将返回一个布尔值,指示输入是否有效。这是一个可以玩的小提琴:
And the code:
和代码:
<input id="subscribe-email" type="email" required="required" placeholder="[email protected]" />
<button type="submit" id="subscribe-submit" onClick="check()">
click me
</button>
function check()
{
if(!document.getElementById("subscribe-email").checkValidity())
{
//do stuff here ie. show errors
alert("input not valid!");
}else
{
callMeIfValid();
}
}
function callMeIfValid()
{
//submit form or whatever
alert("valid input");
}
回答by Bigood
You can use Regular expressionsto check that the email is valid on your omgemailgone()
:
您可以使用正则表达式来检查电子邮件在您的omgemailgone()
:
function omgemailgone (){
var mail = $('#subscribe-email').val();
//Example of regular expression
if(mail.match(/YourRegexp/)
{
//Do stuff
}
else alert("Invalid e-mail");
}
(using jQuery here)
(此处使用 jQuery)
回答by Rice_Crisp
check Validate email address in JavaScript?for validation and then implement it into an if statement in your omgemailgone method (if valid continue, else do nothing)
检查在 JavaScript 中验证电子邮件地址?进行验证,然后将其实现到 omgemailgone 方法中的 if 语句中(如果有效则继续,否则什么都不做)
edit:
编辑:
function validateEmail(email) {
var re = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\
".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA
-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
from the link
从链接
回答by Keo Strife
u need a function that validate the email and return true or false Validate email address in JavaScript?
你需要一个函数来验证电子邮件并返回 true 或 false 在 JavaScript 中验证电子邮件地址?
<button type="submit" id="subscribe-submit" onClick="javascript:validateEmail(document.getElementById('subscribe-email').value) ? omgemailgone() : alert('email is wrong dude');" />
This is a quick solution, i recommend you to do it properly, not using inline onclick js
这是一个快速的解决方案,我建议您正确执行,不要使用内联 onclick js