[英]Regular expression for email not working
我正在對電子郵件字段進行驗證,但即使以正確格式輸入電子郵件,它也會顯示無效錯誤。
代碼
<script type="text/javascript">
function formvalidate(){
var email=document.signup_form.email.value;
var check_email= RegExp("^[A-Z0-9._-]+@[A-Z0-9.-]+\.[A-Z0-9.-]+$");
if(email=="")
{
alert("cannot be empty!");
document.signup_form.email.focus();
return false;
}
else if(!check_email.test(email))
{
alert("enter valid email address!");
document.signup_form.email.focus();
return false;
}
else
{
return true;
}
}
</script>
謝謝
1- 改用這個正則表達式
\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
2- 將此if (!check_email.test(email))
更改為
if (check_email.test(email))
試試這個功能
function validateEmail(elementValue) {
var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
return emailPattern.test(elementValue);
}
請參考這個小提琴: http : //jsfiddle.net/gabrieleromanato/Ra85j/
試試這個功能
function isEmail(inputString) {
var regExpEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
try {
return regExpEmail.test(inputString.value);
}
catch (e) {
return false;
}
}
更改var check_email= RegExp("^[A-Z0-9._-]+@[A-Z0-9.-]+\\.[A-Z0-9.-]+$");
到這樣的功能:
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);
}
你的正則表達式不正確,有很多你沒有考慮過的事情,比如你的正則表達式只接受大寫字母,包括你應該使用的大寫和小寫字母:
[a-zA-Z0-9]
不是這個:
[A-Z0-9]
您可以使用此正則表達式來驗證電子郵件:
/^(([^<>()\[\]\\.,;:\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,}))$/
閱讀本文了解詳情
試試這個例子: email validation using JQuery
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); } function validate() { $("#emailvalidate").text(""); var email = $("#email").val(); if (validateEmail(email)) { $("#emailvalidate").text(email + " is valid"); $("#emailvalidate").css("color", "green"); } else { $("#emailvalidate").text(email + " is not valid"); $("#emailvalidate").css("color", "red"); } return false; } $("form").bind("submit", validate);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <p>Enter an email address:</p> <input id='email'> <button type='submit' id='btn'>Validate</button> </form> <h2 id='emailvalidate'></h2>
試試這個代碼 -
function formvalidate() { var email = document.signup_form.email.value; var check_email = RegExp("^([a-z0-9\\\\+_\\\\-]+)(\\\\.[a-z0-9\\\\+_\\\\-]+)*@([a-z0-9\\\\-]+\\\\.)+[az]{2,6}$", 'ig'); if(email == "") { alert("cannot be empty!"); document.signup_form.email.focus(); return false; } else if(!check_email.test(email)) { alert("enter valid email address!"); document.signup_form.email.focus(); return false; } else { return true; } }
<form name="signup_form"> <input type="text" name="email" value="" /> <input type="button" name="validate" value="Validate" onclick="formvalidate()"/> </form>
將您的正則表達式更改為以下
^[a-zA-Z0-9._-]+@[A-Za-z0-9.-]+\\.[a-zA-Z0-9.-]+$
希望這有幫助:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.