文本框的电子邮件验证 [英] Email validation for textbox
本文介绍了文本框的电子邮件验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我编写了以下代码来验证textbox
的电子邮件.它不允许DOT(.),但是我只允许一次DOT(.).我应该在以下代码中删除哪一个?
I wrote the following code to validate the textbox
for email. It''s not allowing DOT(.) but I want to allow the DOT(.) only once. Which one should I delete in the following code?
if(document.getElementById(txtemail).value=="")
{
alert(Email id can not be blank)
document.getElementById(txtemail).focus();
return false;
}
var emailPat = /^(\.*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
var emailid=document.getElementById(txtemail).value;
var matchArray = emailid.match(emailPat);
if (matchArray == null)
{
alert("Your email address seems incorrect. Please try again.);
document.getElementById(txtEmail).focus();
return false;
}
}
推荐答案
/; var emailid = document.getElementById(txtemail).value; var matchArray = emailid.match(emailPat); 如果(matchArray == null) { alert("您的电子邮件地址似乎不正确.请再次返回 假; } }
/; var emailid=document.getElementById(txtemail).value; var matchArray = emailid.match(emailPat); if (matchArray == null) { alert("Your email address seems incorrect. Please try again.); document.getElementById(txtEmail).focus(); return false; } }
您的验证表达式错误.
在一个以下使用
Your validation expression is wrong.
Use below one
var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)
/
或
请参阅下面的JavaScript代码以验证电子邮件ID.我们主要使用它
Or
See below JavaScript code to validate email id. We mostly use it
<script language="Javascript">
function echeck(str) {
var at="@"
var dot="."
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
if (str.indexOf(at)==-1){
alert("Invalid E-mail ID")
return false
}
if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
alert("Invalid E-mail ID")
return false
}
if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
alert("Invalid E-mail ID")
return false
}
if (str.indexOf(at,(lat+1))!=-1){
alert("Invalid E-mail ID")
return false
}
if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
alert("Invalid E-mail ID")
return false
}
if (str.indexOf(dot,(lat+2))==-1){
alert("Invalid E-mail ID")
return false
}
if (str.indexOf(" ")!=-1){
alert("Invalid E-mail ID")
return false
}
return true
}
function ValidateForm(){
var emailID=document.frmSample.txtEmail
if ((emailID.value==null)||(emailID.value=="")){
alert("Please Enter your Email ID")
emailID.focus()
return false
}
if (echeck(emailID.value)==false){
emailID.value=""
emailID.focus()
return false
}
return true
}
</script>
希望对您有帮助:)
Hope this may help you :)
这篇关于文本框的电子邮件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文