AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > JavaScript

js与jquery正则验证电子邮箱、手机号、邮政编码的方法

51自学网 http://www.wanshiok.com
js,jquery,正则验证,电子邮箱,手机号,邮政编码

本文实例讲述了js与jquery正则验证电子邮箱、手机号、邮政编码的方法。

jQuery代码:

//验证邮政编码 $("#postcode").blur(function(){  //获取邮政编码  var postcode=$("#postcode").val();  if(is_postcode(postcode)){   $("#postcode_info").html("");  }else{   $("#postcode_info").html("邮编格式不正确");   return false;  } }); //验证手机号码 $("#mobile").blur(function(){  //获取手机号,并去除左右两边空格  var mobile=$.trim($("#mobile").val());  if(is_mobile(mobile)){   $("#mobile_info").html("");  }else{   $("#mobile_info").html("手机号格式不正确");   return false;  } }); //验证email $("#email").blur(function(){  //获取email  var email=$("#email").val();  if(is_email(email)){   $("#email_info").html("");  }else{   $("#email_info").html("电子邮件格式不正确");   return false;  } });});

js代码:

//订单提交页-验证邮政编码function is_postcode(postcode) { if ( postcode == "") {  return false; } else {  if (! /^[0-9][0-9]{5}$/.test(postcode)) {   return false;  } } return true;}//订单提交页-验证手机号function is_mobile(mobile) {  if( mobile == "") {  return false;  } else {  if( ! /^0{0,1}(13[0-9]|15[0-9]|18[0-9]|14[0-9])[0-9]{8}$/.test(mobile) ) {  return false;  }  return true; }}//订单提交页-验证email的合法性function is_email(email) { if ( email == "") {  return false; } else {  if (! /^[/w/-/.]+@[/w/-/.]+(/./w+)+$/.test(email)) {   return false;  } } return true;}

PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:

JavaScript正则表达式在线测试工具:
http://tools.wanshiok.com/regex/javascript

正则表达式在线生成工具:
http://tools.wanshiok.com/regex/create_reg

希望本文所述对大家javascript程序设计有所帮助。


js,jquery,正则验证,电子邮箱,手机号,邮政编码  
上一篇:js中使用使用原型(prototype)定义方法的好处详解  下一篇:浅谈js构造函数的方法与原型prototype