正则表达式实现邮箱验证的功能

前端之家收集整理的这篇文章主要介绍了正则表达式实现邮箱验证的功能前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

.HTML代码如下:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html>
  3.  
  4. <head>
  5. <Meta http-equiv="content-type" content="text/html;charset=gbk"/>
  6. <title>RegExp用法</title>
  7.  
  8. <script type="text/javascript">
  9. function chkEmail(){
  10. var email = document.getElementById('email').value;
  11. var obj = document.getElementById('showMess');
  12. //www@163.com
  13. var pattern = /^[a-zA-Z]\w+[@]\w+[.][\w.]+$/;//<pre name="code" class="html">^[a-zA-Z]<span style="font-family: Arial,Helvetica,sans-serif;">表示以字母开头</span>
if(pattern.test(email)){obj.innerText = "正确";obj.style.color = 'green';}else{obj.innerText = "错误";obj.style.color = 'red';}}</script></head><body><label>电子邮件:</label><input type="text" id="email" onblur="chkEmail()"/><span id="showMess"></span></body></html>
  1.  
结果如下:

猜你在找的正则表达式相关文章