exp.validate.js

简单实用的js基础数据验证

prototype

  1 /// <reference path="/Scripts/expand-fn/exp_validate.js" />
  2 /*数据验证 liuph 2015-03-03
  3 扩展String方法
  4 */
  5 
  6 //验证整数
  7 String.prototype.exp_isInt = function () {
  8     return /^[+-]?\d+$/.test(this);
  9 }
 10 //验证正整数
 11 String.prototype.exp_isIntT = function () {
 12     return /^\d+$/.test(this) && this != 0;
 13 }
 14 //验证非负整数
 15 String.prototype.exp_isIntN = function () {
 16     return /^\d+$/.test(this);
 17 }
 18 //验证负整数
 19 String.prototype.exp_isIntF = function () {
 20     return /^-\d+$/.test(this);
 21 }
 22 //验证小数
 23 String.prototype.exp_isFloat = function () {
 24     return /^[+-]?\d+(.\d+)*$/.test(this);
 25 }
 26 //验证正小数
 27 String.prototype.exp_isFloatT = function () {
 28     return /^\d+(.\d+)*$/.test(this) && this != 0;
 29 }
 30 //验证非负小数
 31 String.prototype.exp_isFloatN = function () {
 32     return /^\d+(.\d+)*$/.test(this);
 33 }
 34 //验证负小数
 35 String.prototype.exp_isFloatF = function () {
 36     return /^-\d+(.\d+)*$/.test(this);
 37 }
 38 
 39 //中文
 40 String.prototype.exp_isCN = function () {
 41     return /^[\u4e00-\u9fa5]+$/.test(this);
 42 }
 43 //英文
 44 String.prototype.exp_isEN = function () {
 45     return /^[a-zA-Z]+$/.test(this);
 46 }
 47 //数字
 48 String.prototype.exp_isNum = function () {
 49     return /^[0-9]+$/.test(this);
 50 }
 51 //中文英文数字
 52 String.prototype.exp_isStr = function () {
 53     return /^[\u4e00-\u9fa5a-zA-Z0-9]+$/.test(this);
 54 }
 55 //中文英文数字下划线横线
 56 String.prototype.exp_isStr1 = function () {
 57     return /^[\u4e00-\u9fa5a-zA-Z0-9\_\-]+$/.test(this);
 58 }
 59 
 60 //手机号码(需更新)
 61 String.prototype.exp_isPhone = function () {
 62     return /^1((([3,8][0-9]|[5][0-3,5-9]|[7][6-8])[0-9]{8})|[7][0][0,5,9][0-9]{7})$/.test(this);
 63 }
 64 //电话 区号-电话-分机号 分机号可不写
 65 String.prototype.exp_isTel = function () {
 66     return /^[0][1-9][0-9]{1,2}[\-\*]?[1-9][0-9]{6,7}([-][0-9]+)*$/.test(this);
 67 }
 68 //电子邮箱
 69 String.prototype.exp_isEmail = function () {
 70     return /^[a-zA-Z0-9\_\.]+@[a-zA-Z0-9]+\.[a-zA-Z]+(\.[a-zA-Z]+)*$/.test(this);
 71 }
 72 
 73 //长度 min最小长度(包含) max最大长度(包含)
 74 String.prototype.exp_isLength = function (min, max) {
 75     var valMin = min == null ? true : this.length > min - 1;
 76     var valMax = max == null ? true : this.length < max + 1;
 77     return valMin && valMax;
 78 }
 79 //去除前后空格
 80 String.prototype.exp_Trim = function () {
 81     return this.replace(/(^\s*)|(\s*$)/g, "");
 82 }
 83 //去除前空格
 84 String.prototype.exp_TrimL = function () {
 85     return this.replace(/^\s/g, "");
 86 }
 87 //去除后空格
 88 String.prototype.exp_TrimR = function () {
 89     return this.replace(/\s*$/g, "");
 90 }
 91 //去除所有空格
 92 String.prototype.exp_TrimA = function () {
 93     return this.replace(/\s/g, "");
 94 }
 95 //密码强度 0空/1弱/2中/3强
 96 String.prototype.exp_PwdLevel = function (pwd) {
 97     var num = pwd.replace(/[^0-9]/g, "").length;/*数字个数*/
 98     var en = pwd.replace(/[^a-zA-Z]/g, "").length;/*字母个数*/
 99     var punctuation = pwd.replace(/[0-9a-zA-Z]/g, "").length;/*标点个数*/
100     return (num > 0 ? 1 : 0) + (en > 0 ? 1 : 0) + (punctuation > 0 ? 1 : 0)
101 }
102 //获取地址栏值 null
103 String.prototype.exp_GetQueryString =function GetQueryString(name) {
104     var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
105     var r = window.location.search.substr(1).match(reg);
106     if (r != null) return unescape(r[2]); return null;
107 }
exp_validate.js

 

 function

  1 /// <reference path="/Scripts/expand-fn/exp.validate.js" />
  2 /*数据验证 liuph 2015-03-03
  3 直接使用全局变量expV.方法
  4 */
  5 var expV = {};
  6 //验证整数
  7 expV.isInt = function (str) {
  8     return /^[+-]?\d+$/.test(str);
  9 }
 10 //验证正整数
 11 expV.isIntT = function (str) {
 12     return /^\d+$/.test(str) && str != 0;
 13 }
 14 //验证非负整数
 15 expV.isIntN = function (str) {
 16     return /^\d+$/.test(str);
 17 }
 18 //验证负整数
 19 expV.isIntF = function (str) {
 20     return /^-\d+$/.test(str);
 21 }
 22 //验证小数
 23 expV.isFloat = function (str) {
 24     return /^[+-]?\d+(.\d+)*$/.test(str);
 25 }
 26 //验证正小数
 27 expV.isFloatT = function (str) {
 28     return /^\d+(.\d+)*$/.test(str) && str != 0;
 29 }
 30 //验证非负小数
 31 expV.isFloatN = function (str) {
 32     return /^\d+(.\d+)*$/.test(str);
 33 }
 34 //验证负小数
 35 expV.isFloatF = function (str) {
 36     return /^-\d+(.\d+)*$/.test(str);
 37 }
 38 
 39 //中文
 40 expV.isCN = function (str) {
 41     return /^[\u4e00-\u9fa5]+$/.test(str);
 42 }
 43 //英文
 44 expV.isEN = function (str) {
 45     return /^[a-zA-Z]+$/.test(str);
 46 }
 47 //数字
 48 expV.isNum = function (str) {
 49     return /^[0-9]+$/.test(str);
 50 }
 51 //中文英文数字
 52 expV.isStr = function (str) {
 53     return /^[\u4e00-\u9fa5a-zA-Z0-9]+$/.test(str);
 54 }
 55 //中文英文数字下划线横线
 56 expV.isStr1 = function (str) {
 57     return /^[\u4e00-\u9fa5a-zA-Z0-9\_\-]+$/.test(str);
 58 }
 59 
 60 //手机号码(需更新)
 61 expV.isPhone = function (str) {
 62     return /^1((([3,8][0-9]|[5][0-3,5-9]|[7][6-8])[0-9]{8})|[7][0][0,5,9][0-9]{7})$/.test(str);
 63 }
 64 //仅作宽松验证
 65 expV.isPhone1 = function (str) {
 66     return /^1((([3,8][0-9]|[5][0-3,5-9]|[7][6-8])[0-9]{8})|[7][0][0,5,9][0-9]{7})$/.test(str);
 67 }
 68 //电话 区号-电话-分机号 分割用-或* 分机号可不写
 69 expV.isTel = function (str) {
 70     return /^[0][1-9][0-9]{1,2}[\-\*]?[1-9][0-9]{6,7}([-][0-9]+)*$/.test(str);
 71 }
 72 //电子邮箱
 73 expV.isEmail = function (str) {
 74     return /^[a-zA-Z0-9\_\.]+@[a-zA-Z0-9]+\.[a-zA-Z]+(\.[a-zA-Z]+)*$/.test(str);
 75 }
 76 
 77 //长度 min最小长度(包含) max最大长度(包含)
 78 expV.isLength = function (str, min, max) {
 79     var valMinmin = (min == null ? true : str.length > min - 1);
 80     var valMaxmax = (max == null ? true : str.length < max + 1);
 81     return valMin && valMax;
 82 }
 83 //去除前后空格
 84 expV.Trim = function (str) {
 85     return str.replace(/(^\s*)|(\s*$)/g, "");
 86 }
 87 //去除前空格
 88 expV.TrimL = function (str) {
 89     return str.replace(/^\s/g, "");
 90 }
 91 //去除后空格
 92 expV.TrimR = function (str) {
 93     return str.replace(/\s*$/g, "");
 94 }
 95 //去除所有空格
 96 expV.TrimA = function (str) {
 97     return str.replace(/\s/g, "");
 98 }
 99 //密码强度 0空/1弱/2中/3强
100 expV.PwdLevel = function PwdLevel(pwd) {
101     var num = pwd.replace(/[^0-9]/g, "").length;/*数字个数*/
102     var en = pwd.replace(/[^a-zA-Z]/g, "").length;/*字母个数*/
103     var punctuation = pwd.replace(/[0-9a-zA-Z]/g, "").length;/*标点个数*/
104     return (num > 0 ? 1 : 0) + (en > 0 ? 1 : 0) + (punctuation > 0 ? 1 : 0)
105 }
106 //获取地址栏值 null
107 expV.GetQueryString = function GetQueryString(name) {
108     var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
109     var r = window.location.search.substr(1).match(reg);
110     if (r != null) return unescape(r[2]); return null;
111 }
exp.validate.js

 

posted @ 2015-03-04 10:04  灰色雨逸  阅读(261)  评论(0编辑  收藏  举报