[代码] 方法1
01 | <html> |
02 | <head> |
03 | <title>Checking an email address - Version 01</title> |
04 | <script type= "text/javascript" language= "javascript" > |
05 | <!-- // |
06 | function IsMatchingAddress(str){ |
07 | var myRegExp = /[a-z0-9-]{1,30}@[a-z0-9-]{1,65}.[a-z]{3}/ ; |
08 | return myRegExp.test(str) |
09 | } |
10 |
11 | function TestGuess(){ |
12 |
13 | var EmailAddr = "asdf@asdf.net" .toLowerCase(); |
14 | alert(IsMatchingAddress(EmailAddr)); |
15 | } |
16 | // --> |
17 | </script> |
18 | </head> |
19 | <body> |
20 | <h3>This page allows you to enter and check |
21 |
an email address such as asdf@java2s.com.com</h3> |
22 | <form> |
23 | <button type= "Button" onclick= "TestGuess()" > |
24 | Click 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 | <!-- // |
06 | function IsMatchingAddress(str){ |
07 | var myRegExp = /[a-z0-9-.]{1,30}@[a-z0-9-]{1,65}.(com|net|org|info|biz|([a-z]{2,3}.[a-z]{2}))/ ; |
08 | return myRegExp.test(str) |
09 | } |
10 |
11 | function TestGuess(){ |
12 | var EmailAddr = "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 |
20 |
such as asdf@java2s.com,</h3> |
21 | <form> |
22 | <button type= "Button" onclick= "TestGuess()" > |
23 | Click 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 | function isValidEmail(sText) { |
06 | var reEmail = /^(?:\w+\.?)*\w+@(?:\w+\.)+\w+$/; |
07 | return reEmail.test(sText); |
08 | } |
09 | function validate() { |
10 | var oInput1 = 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> |