java和javascript的数字校验
2015-08-16 12:58
771 查看
java和javascript的数据验证
java: package lottery.web.common; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * * @author zhx creation date:2007-11-9 * */ public class Util { /** * 身份证校验位 */ public static String[] CHECK_DIGIT={"1","0","X","9","8","7","6","5","4","3","2"}; /** * 身份证加权因子 */ public static int[] gene={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1}; /** * 获得一个字符串的长度,一个汉字按2个长度算 * @param str * @return */ public static int getLengthAsLetter(String str) { int len = 0; int strLength = str.length(); int code; for (int i = 0; i < strLength; i++) { code = str.charAt(i); if (code > 0 && code < 257) { len++; } else { len += 2; } } return len; } /** * 判断是否为email * @param email * @return 是email返回true; 不是email返回false */ public static boolean isEmail(String email) { if(email==null||email.equals(""))return false; Pattern pattern = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"); Matcher isMail = pattern.matcher(email); return isMail.matches(); } /** * 判断字符串是否为数字 * @param email * @return 是数字返回true; 不是数字返回false */ public static boolean isNum(String num) { if(num==null ||num.equals(""))return false; Pattern pattern = Pattern.compile("[0-9]*"); Matcher isNum = pattern.matcher(num); return isNum.matches(); } /** * 判断一个字符串是否为合法的字符 * @param temp * @return */ public static boolean isLegalityLetter(String temp) { if(temp==null)return false; if(temp.equals(""))return true; Pattern pattern=Pattern.compile("^//w+$"); Matcher isLegalityLetter=pattern.matcher(temp); return isLegalityLetter.matches(); } /** * 将一个字符串式的日期格式转换为日期型的数据 * @param temp * @return */ public static Date parseString(String temp) { return null; } /** * 获得某个日期的月上减去monthCount个月 * @param date * @param monthCount * @return */ public static Date parseByMonthCount(Date date,Integer monthCount) { if(date==null)return null; if(monthCount==null) return null; Calendar calendar=new GregorianCalendar(); calendar.setTime(date); calendar.add(calendar.MONTH,monthCount); return calendar.getTime(); } /** * 判断是否为用户名 * @param lName * @return */ public static boolean isLoginName(String lName) { if(lName==null||lName.equals(""))return false; Pattern pattern=Pattern.compile("^[//w@//.-]{8,26}$"); Matcher isLoginName=pattern.matcher(lName); return isLoginName.matches(); } /** * 判断是否为手机号 */ public static boolean isMobile(String mo) { if(mo==null||mo.equals(""))return false; Pattern pattern=Pattern.compile("^1[3|4|5|7|8][0-9]\\d{8}$"); Matcher isMobile=pattern.matcher(mo); return isMobile.matches(); } /** * 判断是否为身份证号^(/d{15}|(/d{17}[xX/d]))$ */ public static boolean isIdentityCard(String card) { // if(card==null||card.equals(""))return false; // if(card.length()!=15&&card.length()!=18)return false; // Pattern pattern=Pattern.compile("^(//d{15}|(//d{17}[xX//d]))$"); // Matcher isIdentityCard=pattern.matcher(card); // return isIdentityCard.matches(); if(card==null||card.equals(""))return false; if(card.length()!=15&&card.length()!=18)return false; Pattern pattern=Pattern.compile("^(//d{15}|(//d{17}[xX//d]))$"); Matcher isIdentityCard=pattern.matcher(card); if(!isIdentityCard.matches()) return false; if(card.length()==18) { int yearPrefix=Integer.parseInt(card.substring(6,8)); if(yearPrefix<19||yearPrefix>21)return false;//出生日期必须大于1900年小于2100年 int month=Integer.parseInt(card.substring(10,12)); if(month>12||month==0)return false; //验证月 int day=Integer.parseInt(card.substring(12,14)); if(day>31||day==0)return false; //验证日 String checkDigit=getCheckDigitFor18(card); if(checkDigit.equals("-1"))return false; if(checkDigit.equals(card.substring(17,18).toUpperCase())) { return true; }else { return false; } }else if(card.length()==15) { int month=Integer.parseInt(card.substring(8,10)); if(month>12||month==0)return false; //验证月 int day=Integer.parseInt(card.substring(10,12)); if(day>31||day==0)return false; return true; } return false; } private static String getCheckDigitFor18(String card) { if(card==null||card.equals(""))return "-1"; int sum=0; for(int i=0;i<17;i++) { sum+=Integer.parseInt(card.substring(i,i+1))*gene[i]; } return CHECK_DIGIT[sum%11]; } /** * 随机字符串生成 * @return */ public static String getRandmStr(int length) { char[] tempCs= { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm' }; Random random = new Random(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < length; i++) { sb.append(tempCs[Math.abs(random.nextInt())%tempCs.length]); } return sb.toString(); } } javascript //---------------------------------String的扩展方法列表------------------------------------------ // author: zhx // trim() 去除两边空格 // lTrim() 去除左空格 // rTrim() 去除右空格 // realLength() 返回字符串的长度,一个汉字数3个字符 // isInteger() 是否为整数 是则返回true 不是则返回false // isDouble() 是否为浮点数 是则返回true 不是则返回false // isDate() 是否为有效日期 是则返回true 不是则返回false // isEmpty() 是否为空 是则返回true 不是则返回false // isEmail) 判断Email是否有效 // isAllChinese() 判断字符串是否全部是汉字 // isLetterOrNumber() 判断字符串是否全部是字符、数字、下划线组成 是则返回true 不是则返回false // toDate() 将字符串转换成Date //-------------------------------------------------------------------------------------------------- String.prototype.trim = function() { return this.replace(/(^[/s]*)|([/s]*$)/g, ""); } String.prototype.lTrim = function() { return this.replace(/(^[/s]*)/g, ""); } String.prototype.rTrim = function() { return this.replace(/([/s]*$)/g, ""); } /** * 计算字符串的长度,一个汉字两个字符 */ String.prototype.realLength = function() { return this.replace(/[^/x00-/xff]/g,"**").length; } /** *校验字符串是否为整型 *返回值: *如果为空,定义校验通过, 返回true *如果字串全部为数字,校验通过,返回true *如果校验不通过, 返回false 参考提示信息:输入域必须为数字! */ String.prototype.isInteger = function() { //如果为空,则通过校验 if(this == "") return false; if(/^(/-?)(/d+)$/.test(this)) return true; else return false; } /** *校验字符串是否为浮点型 *返回值: *如果为空,定义校验通过, 返回true *如果字串为浮点型,校验通过, 返回true *如果校验不通过, 返回false 参考提示信息:输入域不是合法的浮点数! */ String.protot f957 ype.isDouble = function() { //如果为空,则通过校验 if(this == "") return false; //如果是整数,则校验整数的有效性 if(this.indexOf(".") == -1) { if(this.isInteger(this) == true) return true; else return false; } else { if(/^(/-?)(/d+)(.{1})(/d+)$/g.test(this)) return true; else return false; } } /** *校验字符串是否为日期型 *返回值: *如果为空,定义校验通过, 返回true *如果字串为日期型,校验通过, 返回true *如果日期不合法, 返回false 参考提示信息:输入域的时间不合法!(yyyy-MM-dd) */ String.prototype.isDate = function() { //如果为空,则通过校验 if(this == "") return true; if(/^(/d{4})-(/d{1,2})-(/d{1,2})$/.test(this)){ return true; } return false; } /** *校验字符串是否为空 *返回值: *如果不为空,定义校验通过,返回true *如果为空,校验不通过,返回false 参考提示信息:输入域不能为空! */ String.prototype.isEmpty = function() { if(this.trim() == "") return true; else return false; } /** *校验字符串是否为email型 *返回值: *如果为空,定义校验通过, 返回true *如果字串为email型,校验通过, 返回true *如果email不合法, 返回false 参考提示信息:Email的格式不正?_! */ String.prototype.isEmail = function() { if(this == "") return false; if(/^/w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*$/.test(this)) return true; else return false; } /** *校验字符串是否为中文 *返回值: *如果为空,定义校验通过, 返回true *如果字串为中文,校验通过, 返回true *如果字串为非中文, 返回false 参考提示信息:必须为中文! */ String.prototype.isAllChinese = function() { //如果值为空,通过校验 if (this == "") return true; var pattern = /^([/u4E00-/u9FA5]|[/uFE30-/uFFA0])*$/gi; if (pattern.test(this)) return true; else return false; } /** *校验字符串是否为中文 *返回值: *如果为空,定义校验通过, 返回true *如果字串为中文,校验通过, 返回true *如果字串为非中文, 返回false 参考提示信息:必须为中文! */ String.prototype.isLetterOrNumber = function() { //如果值为空,通过校验 if (this == "") return true; var pattern =/^/w+$/gi; if (this.match(pattern)) return true; else return false; } String.prototype.isMobile = function() { if(this == "") return false; if(/^1[35]/d{9}$/.test(this)) return true; else return false; } String.prototype.isLoginName=function() { if(this=="")return false; if(/^[/w@/.-]{8,26}$/g.test(this)) return true; else return false; } function getCheckDigitFor18(card) { var CHECK_DIGIT=new Array("1","0","X","9","8","7","6","5","4","3","2"); var gene=new Array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1); if(!card||card=="")return "-1"; var sum=0; for(var i=0;i<17;i++) { sum+=parseInt(card.substr(i,1))*gene[i]; } return CHECK_DIGIT[sum%11]; } /** *判断是否为身份证号 */ String.prototype.isIdentityCard=function() { //if(this=="")return false; //if(/^(/d{15}|(/d{17}[xX/d]))$/.test(this)) //{ // return true; //}else //{ // return false; //} if(this=="")return false; if(this.length!=15&&this.length!=18)return false; if(!(/^(/d{15}|(/d{17}[xX/d]))$/.test(this))) return false; if(this.length==18) { var yearPrefix=parseInt(this.substr(6,2)); if(yearPrefix<19||yearPrefix>21)return false; //出生日期必须大于1900年小于2100年 var month=parseInt(this.substr(10,2)); if(month>12||month==0)return false; //验证月 var day=parseInt(this.substr(12,2)); if(day>31||day==0)return false; //验证日 var checkDigit=getCheckDigitFor18(this); if(checkDigit=="-1")return false; if(checkDigit==this.substr(17,1).toUpperCase()) { return true; }else { return false; } }else if(this.length==15) { var month=parseInt(this.substr(8,2)); if(month>12||month==0)return false; //验证月 var day=parseInt(this.substr(10,2)); if(day>31||day==0)return false; return true; } return false; } |
相关文章推荐
- JavaScript基础学习之-JavaScript权威指南--3.8.3对象转换成原始值
- js捕获键盘按键值,并且实现焦点切换
- 《JavaScript》——DOM
- 【JS进阶之路】JavaScript之入门初识
- javascript中 try catch用法
- JavaScript全局函数
- js中判断浏览器的类别
- JavaScript命名空间的理解与实现
- 第一篇 arcgis api for javascript 简单地图
- javascript设计模式之单例(singleton)模式
- js-判断字符是否为数字
- servlet和jsp存值和取值的方式
- js遇到的一些小问题心得
- JavaScript学习全栈教程-分享
- javascript对象
- javascript中undefined与null的区别
- 【JSOI2008】【BZOJ1036】【LCT版】树的统计Count
- 倒计时js代码
- jsp filter字符过滤器
- js在web绘制在页上的圆