JavaScript 检查 email 地址的正则表达式

[代码] 方法1

01<html>
02<head>
03<title>Checking an email address - Version 01</title>
04<script type="text/javascript"language="javascript">
05<!-- //
06function IsMatchingAddress(str){
07    varmyRegExp = /[a-z0-9-]{1,30}@[a-z0-9-]{1,65}.[a-z]{3}/ ;
08    returnmyRegExp.test(str)
09}
10 
11function TestGuess(){
12 
13var EmailAddr = "asdf@asdf.net".toLowerCase();
14alert(IsMatchingAddress(EmailAddr));
15}
16// -->
17</script>
18</head>
19<body>
20<h3>This page allows you to enter and check
21an email address such as asdf@java2s.com.com</h3>
22<form>
23<button type="Button"onclick="TestGuess()">
24Click here to enter email address</button>
25</form>
26</body>
27</html>

[代码] 方法2

01<html>
02<head>
03<title>Checking an email address - Version 02</title>
04<script type="text/javascript"language="javascript">
05<!-- //
06function IsMatchingAddress(str){
07    varmyRegExp = /[a-z0-9-.]{1,30}@[a-z0-9-]{1,65}.(com|net|org|info|biz|([a-z]{2,3}.[a-z]{2}))/ ;
08    returnmyRegExp.test(str)
09}
10 
11function TestGuess(){
12    varEmailAddr = "asdf@java2s.com".toLowerCase();
13    alert(IsMatchingAddress(EmailAddr));
14}
15// -->
16</script>
17</head>
18<body>
19<h3>This page allows you to enter and check an email address
20such as asdf@java2s.com,</h3>
21<form>
22<button type="Button"onclick="TestGuess()">
23Click here to enter email address</button>
24</form>
25</body>
26</html>

[代码] 方法3

01<html>
02<head>
03<title>E-mail Example</title>
04<script type="text/javascript">
05    functionisValidEmail(sText) {
06        varreEmail = /^(?:\w+\.?)*\w+@(?:\w+\.)+\w+$/;
07        returnreEmail.test(sText);
08    }
09    functionvalidate() {
10        varoInput1 = document.getElementById("txt1");
11        if(isValidEmail(oInput1.value)) {
12            alert("Valid");
13        }else {
14            alert("Invalid!");
15        }
16 
17    }
18</script>
19</head>
20<body>
21    <P>E-mail Address: <input type="text"id="txt1" /><br />
22    <input type="button"value="Validate"onclick="validate()"/></p>
23 
24</body>
25</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值