验证手机号邮箱,多个以英文逗号分隔

package com.test;

import java.util.regex.Pattern;

public class TestRegex {
	/**
	 * 正则表达式:验证单个手机号
	 */
	public static final String REGEX_MOBILE = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";

	/**
	 * 正则表达式:验证手机号,多个以英文逗号分隔
	 */
	public static final String REGEX_MOBILE_MULTI = "^1[3578][0-9]{9}(,1[3578][0-9]{9})*$";

	/**
	 * 正则表达式:验证单个邮箱(允许英文大小写字母、数字、下划线、英文句号、以及中划线组成)
	 */
	public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

	/**
	 * 正则表达式:验证邮箱,多个以英文逗号分隔
	 */
	public static final String REGEX_EMAIL_MULTI = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}(,([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,})*$";

	/**
	 * 校验手机号
	 * 
	 * @param mobile
	 * @return 校验通过返回true,否则返回false
	 */
	public static boolean isMobile(String mobile) {
		// return Pattern.matches(REGEX_MOBILE, mobile);
		return Pattern.matches(REGEX_MOBILE_MULTI, mobile);
	}

	/**
	 * 校验邮箱
	 * 
	 * @param email
	 * @return 校验通过返回true,否则返回false
	 */
	public static boolean isEmail(String email) {
		// return Pattern.matches(REGEX_EMAIL, email);
		return Pattern.matches(REGEX_EMAIL_MULTI, email);
	}

	public static void main(String[] args) {
		try {
			// 中文逗号
			String test1 = "1232132144,12333213111";
			// 12位数字
			String test2 = "132132133333";
			// 非法的逗号位置
			String test3 = "13213213333,13213213333,";
			String test4 = ",13213213333,13213213333";
			// 非逗号分隔
			String test5 = "13213213333 13213213333";
			String test6 = "13213213333.13213213333";
			// 错误的11位号码
			String test7 = "12345678910,18829038107";
			// 包含电话号码
			String test8 = "13213213333,0311-1234567,011-12345678";
			// 正确的格式
			String test9 = "13213213333";
			String test10 = "13213213333,18829038107";

			System.out.println("phone-test1:" + isMobile(test1));
			System.out.println("phone-test2:" + isMobile(test2));
			System.out.println("phone-test3:" + isMobile(test3));
			System.out.println("phone-test4:" + isMobile(test4));
			System.out.println("phone-test5:" + isMobile(test5));
			System.out.println("phone-test6:" + isMobile(test6));
			System.out.println("phone-test7:" + isMobile(test7));
			System.out.println("phone-test8:" + isMobile(test8));
			System.out.println("phone-test9:" + isMobile(test9));
			System.out.println("phone-test10:" + isMobile(test10));

			System.out.println(
					"Email-1:" + isEmail("[email protected][email protected]"));
			System.out.println("Email-2:" + isEmail("fe8-33@@qq.cn"));
			System.out.println(
					"Email-3:" + isEmail("[email protected],[email protected],"));
			System.out.println(
					"Email-4:" + isEmail(",[email protected],[email protected]"));
			System.out.println(
					"Email-5:" + isEmail("[email protected] [email protected]"));
			System.out.println(
					"Email-6:" + isEmail("[email protected]@ren.com.cn"));
			System.out.println(
					"Email-7:" + isEmail("[email protected],[email protected]"));

		} catch (Exception e) {

		}
	}

}

运行结果:

phone-test1:false
phone-test2:false
phone-test3:false
phone-test4:false
phone-test5:false
phone-test6:false
phone-test7:false
phone-test8:false
phone-test9:true
phone-test10:true
Email-1:false
Email-2:false
Email-3:false
Email-4:false
Email-5:false
Email-6:false
Email-7:true

javascript:

<script type="text/javascript">
     	
  	  <#--校验通知地址-->
    function checkNoticeAddress() {
    <#--通知方式1:短信;2:邮件-->
	<#--匹配手机号-->
     	var taskNoticeType = $("#taskNoticeType").val();
     	if( taskNoticeType == '1' ){
     		$("#noticeAddressSpan").html("");
			 var phoneRegx=/^1[3578][0-9]{9}(,1[3578][0-9]{9})*$/;
        	 var blsx = $("#noticeAddress").val();
        	
        	if(blsx==""){
        		$("#noticeAddressSpan").html("提示:通知手机不能为空");
        		
        	}else if (!phoneRegx.test(blsx)) {
            	$("#noticeAddressSpan").html('手机号格式不正确!(请输入11位数字的手机号,多个以逗号分隔)');
            	return false;
        	}else {
            	return true;
        	}
     	}
     	<#--匹配email地址-->
     	if( taskNoticeType == '2' ){
     		$("#noticeAddressSpan").html("");
        	var emailRegx=/^([a-z0-9A-Z]+[-|\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\.)+[a-zA-Z]{2,}(,([a-z0-9A-Z]+[-|\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\.)+[a-zA-Z]{2,})*$/;
        	var blsx = $("#noticeAddress").val();
        	
        	if(blsx==""){
        		$("#noticeAddressSpan").html("提示:通知邮箱不能为空");
        	}else if (!emailRegx.test(blsx)) {
            	$("#noticeAddressSpan").html('邮箱格式不正确!(邮件时为邮箱地址,多个以逗号分隔)');
            	return false;
        	}else {
            	return true;
        	}
     	}
       
    }
    
    <#--校验百分比阈值-->
    function checkThreshold() {
        $("#thresholdSpan").html("");
   
        var thresholdRegx=/^((\+\d+\.?\d*)|(\+\d*\.\d+))$|(((\-\d+\.?\d*)|(\-\d*\.\d+))$)/;
        var blsx = $("#threshold").val();
        
       	if(blsx==""){       	
        	$("#thresholdSpan").html("提示:百分比阈值不能为空");
        }else if (!thresholdRegx.test(blsx)) {
            $("#thresholdSpan").html('百分比阀值格式不正确!(如:+30表示超过30% -30表示下降30%)');
            return false;
        }else {
            return true;
        	}    
    }
 
</script>


猜你喜欢

转载自blog.csdn.net/m0_37721946/article/details/79271910