此电子邮件的RegEx在http://scriptular.com上传递,但所有浏览器都失败。有什么想法吗?我尝试了很多方法;但在这一点上,我一无所知。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>RegEx Test</title>
<script type="text/javascript">
function saveSignUp(){
var warning = "The following field(s) require an entry: \n \n";
var same = warning;
var email = document.forms[0].email.value;
// FIRST TEST
/*var regExEmail= /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+/;*/
// SECOND TEST
var regExEmail= /[a-zA-Z0-9_.-]+@[a-zA-Z0-9_.-]+\.[a-zA-Z]+/;
if(email == ""){ warning += " - Email is required \n"; }
else if(email != regExEmail){ warning += " - E-mail not formatted properly \n"; }
if (warning == same){
return true;
}
else {
alert(warning);
}
return false;
}
</script>
</head>
<body>
<form method="post" onsubmit="return saveSignUp()" action="signupSucces.html" >
<input type="text" placeholder="Email" name="email">
<input type="submit" class="button wide" value="Request an Account">
</form>
</body>
</html>
答案 0 :(得分:2)
if(email != regExEmail)
这不是你测试正则表达式匹配的方法。请改用:
if (!regExEmail.test(email))
并且,你应该在^
.. $
中包含你的正则表达式以检查整个字符串匹配,而不仅仅是它的子字符串。
var regExEmail= /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9_.-]+\.[a-zA-Z]+$/;