首页>代码>域名规则校验Java版本>/1355194245760000.java
package util;

import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DomainCheck {

	/**
	 * 检查域名是否合法
	 * @param domain
	 * @return 返回"LEGAL"成功
	 * 其余就返回不合法消息
	 */
	public static String domainCheckLegal(String domain){
		Pattern p = null;
		Matcher m = null;
		boolean b = false;
		String str = null;
		
		//可以包含中文、字母a-z(大小写等价)、数字0-9或者半角的连接符"-","-"不能放在开头或结尾
		String reg ="^(?!-)(?!.*?-$)[-a-zA-Z0-9\\u4e00-\\u9fa5]*$";

		p = Pattern.compile(reg);
		m = p.matcher(domain);
		b = m.matches();
		
		if(!b){
			return "不符合域名注册规定";
		}
		
		str = m.group();	

		
//		if(str.length()>=3){
//			if(str.substring(2,3).equals("-")){
//				return " “-”符号不能出现在第三和第四位 ";
//			}
//		}
//		if(str.indexOf("-")==2||str.indexOf("-")==3){			
//			return " “-”符号不能出现在第三和第四位 ";
//		}
		
		/*
		reg = "[\\u4e00-\\u9fa5]";//中文必须有1个
		p = Pattern.compile(reg);
		m = p.matcher(str);
		if(!m.find()){
			return "必须有一个中文";
		}
		*/
		
		
		reg = "^[\\u4e00-\\u9fa5]+$";//纯汉字必须大于1位
		p = Pattern.compile(reg);
		m = p.matcher(domain);
		b = m.matches();
		if(b){
			String chinese = m.group();	
			if(chinese.length()<2||chinese.length()>20){
				return "纯中文必须大于1个小于21个";
			}
		}else{
			//判断punycode长度
			if(str.length()<3){
				return "长度必须大于2!";
			}
			
			//如果第一位、二位不是中文,就判断第三、四位是否是“-”
			String str1 = str.substring(0,3);;
			String reg1  ="^[-a-zA-Z0-9]*$";
			p = Pattern.compile(reg1);
			m = p.matcher(str1);
			b = m.matches();

			if(b){
				if(str.indexOf("-")==2||str.indexOf("-")==3){			
					return " “-”符号不能出现在第三和第四位 ";
				}
			}
			//长度为63位
//			str = Punycode.encode(str);//转换成punycode 不用转化
//			if(str.length()>59){
//				return "您输入的域名过长!";
//			}
			
			//判断输入的域名是否超长
			int valueLength = 0;
			String chinese = "[\u4e00-\u9fa5]";  
			for(int i=0;i<str.length();i++){
				String temp = str.substring(i,i+1);
				if(temp.matches(chinese)){
					valueLength += 2;
				}else{
					valueLength++;
				}
			}
			if(valueLength>63){
				return "您输入的域名长度大于63位";
			}
		}
		return "LEGAL";
	}
	
	
	/**
	 * @param args
	 * @throws UnsupportedEncodingException 
	 */
	public static void main(String[] args) throws UnsupportedEncodingException {
		// TODO Auto-generated method stub
//		System.out.println(DomainCheck.domainCheckLegal("s河s北s省s"));;
		String str = "很s好";
		
		int valueLength = 0;
		String chinese = "[\u4e00-\u9fa5]";  
		for(int i=0;i<str.length();i++){
			String temp = str.substring(i,i+1);
			if(temp.matches(chinese)){
				valueLength += 2;
			}else{
				valueLength++;
			}
		}
		System.out.println(valueLength);
		
	}
	
}
最近下载更多
zxyzzy  LV2 2021年4月26日
旧城其209  LV1 2020年8月18日
东北人  LV12 2020年7月27日
信岚先生  LV1 2019年9月12日
wbyMilo  LV1 2019年7月31日
tianheihei  LV1 2019年7月30日
whd1116  LV1 2018年7月12日
924053027  LV1 2018年4月17日
helloboye  LV1 2017年12月21日
xiaozhizhang  LV2 2017年12月11日
最近浏览更多
zxyzzy  LV2 2021年4月26日
旧城其209  LV1 2020年8月18日
东北人  LV12 2020年7月27日
信岚先生  LV1 2019年9月12日
sjd123  LV4 2019年8月27日
凌风丶  LV1 2019年8月1日
wbyMilo  LV1 2019年7月31日
tianheihei  LV1 2019年7月30日
月之氏族  LV23 2019年5月24日
dongzhan  LV12 2019年5月6日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友