用正则表达式搭配JS判断邮件/邮箱地址格式


javascript

用正则表达式判断Email的格式是否正确:


<scriptlanguage=javascript>
function checkEmail(el)//
用正则表达式判断
{
varregu ="^(([0-9a-zA-Z]+)|([0-9a-zA-Z]+[_.0-9a-zA-Z-]*[0-9a-zA-Z-]+))@([a-zA-Z0-9-]+[.])+([a-zA-Z]|net|NET|asia|ASIA|com|COM|gov|GOV|mil|MIL|org|ORG|edu|EDU|int|INT|cn|CN|cc|CC)$"
varre = new RegExp(regu);
if(el.search(re) == -1)
{
returntrue; //
非法
}
returnfalse;//
正确
}

functionchecklogin()
{
var obj =document.getElementById(’emailid’);
if(checkEmail(obj.value))
{
alert("E-mail
格式不正确,请检查!");
obj.focus();
returnfalse;
}
}
</script>

<formname="myform" action="" onSubmit="returnchecklogin();" method="post">
Email:
<inputname="email" id="emailid" type="text"size="25" />
<input name="submit"type="submit" value="
登陆"/></form>

JS判断Email格式是否符合要求:

<scriptlanguage="javascript">
function checkemail()
{
if(document.myform.email.value.charAt(0)=="." ||document.myform.email.value.charAt(0)=="@"||document.myform.email.value.indexOf(’@’, 0) == -1 ||document.myform.email.value.indexOf(’.’, 0) == -1 ||document.myform.email.value.lastIndexOf("@")==document.myform.email.value.length-1||document.myform.email.value.lastIndexOf(".")==document.myform.email.value.length-1)
{
alert("Email
的格式不正确!");
document.myform.email.focus();
returnfalse;
}
return true;
}
</script>

<formname="myform" action="" onSubmit="returncheckemail();" method="post">
Email:
<inputname="email" id="emailid" type="text"size="25" />
<input name="submit"type="submit" value="
登陆"/></form>





Java

publicclassCheckEmail {


    publicstaticbooleancheckEmail(String email){


        //验证邮箱的正则表达式


        Stringformat = "\\p{Alpha}\\w{2,15}[@][a-z0-9]{3,}[.]\\p{Lower}{2,}";


        if(email.matches(format)){


                returntrue;                                 //邮箱名合法,返回true


             }else{


                returnfalse;                            // 邮箱名不合法,返回false


             }


    }


    publicstaticvoidmain(String[] args) {


             Stringemail = "cc**365@163.com";           //需要进行验证的邮箱


             if(CheckEmail.checkEmail(email)){          // 验证邮箱


           System.out.println(email+"\n是合法的邮箱名。");


        }else{


           System.out.println(email+"\n不是合法的邮箱名。");


        }


    }


}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值