正则表达式

来源:互联网 发布:bi数据库设计 编辑:程序博客网 时间:2024/05/18 03:25

Java校验

校验固定电话

校验手机

校验邮编

import java.util.regex.Pattern;/** * 用于csv 导入时的移动电话、固话、邮编的校验 */public class Validate {/**      * 验证手机号码(支持国际格式,+86135xxxx...(中国内地),+00852137xxxx...(中国香港))      * @param mobile 移动、联通、电信运营商的号码段      *<p>移动的号段:134(0-8)、135、136、137、138、139、147(预计用于TD上网卡)      *、150、151、152、157(TD专用)、158、159、187(未启用)、188(TD专用)</p>      *<p>联通的号段:130、131、132、155、156(世界风专用)、185(未启用)、186(3g)</p>      *<p>电信的号段:133、153、180(未启用)、189</p>      * @return 验证成功返回true,验证失败返回false      */  public static boolean isMobile(String mobile){String regex = "(\\+\\d+)?1[3458]\\d{9}$";  return Pattern.matches(regex, mobile);}/** * 区号+座机号码+分机号码 * @param fixedPhone * @return */public static boolean isFixedPhone(String fixedPhone){String reg="(?:(\\(\\+?86\\))(0[0-9]{2,3}\\-?)?([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?)|" +"(?:(86-?)?(0[0-9]{2,3}\\-?)?([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?)";return Pattern.matches(reg, fixedPhone);}/**      * 匹配中国邮政编码      * @param postcode 邮政编码      * @return 验证成功返回true,验证失败返回false      */ public static boolean isPostCode(String postCode){String reg = "[1-9]\\d{5}";return Pattern.matches(reg, postCode);}public static void main(String[] args) {String mobile = "18600000000";boolean ret = isMobile(mobile);System.out.println(ret);String postCode = "110200";ret = isPostCode(postCode);System.out.println(ret);String isFixedPhone = "0571-2234567";ret = isFixedPhone(isFixedPhone);System.out.println(ret);}}


JS校验

校验固定电话

//校验普通电话、传真号码:可以“+”开头,除数字外,可含有“-”    function isFixedPhone(s) {        var result = s.match(/^(?:(0[0-9]{2,3}\-?)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?)?$/);        if (result == null) return false;        return true;    }


校验手机

function is<span style="font-family:Microsoft YaHei;">Mobile</span>(num){     var partten = /^1[3,5,7,8]\d{9}$/;     if(partten.test(num)){     return true     }else{     return false;     }}


校验邮编

 //校验邮政编码    function isPostCode(s) {        var result = s.match(/^[1-9][0-9]{5}$/);        if (result == null) return false;        return true;    }


0 0
原创粉丝点击