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

prototype

 /// <reference path="/Scripts/expand-fn/exp_validate.js" />
/*数据验证 liuph 2015-03-03
扩展String方法
*/ //验证整数
String.prototype.exp_isInt = function () {
return /^[+-]?\d+$/.test(this);
}
//验证正整数
String.prototype.exp_isIntT = function () {
return /^\d+$/.test(this) && this != 0;
}
//验证非负整数
String.prototype.exp_isIntN = function () {
return /^\d+$/.test(this);
}
//验证负整数
String.prototype.exp_isIntF = function () {
return /^-\d+$/.test(this);
}
//验证小数
String.prototype.exp_isFloat = function () {
return /^[+-]?\d+(.\d+)*$/.test(this);
}
//验证正小数
String.prototype.exp_isFloatT = function () {
return /^\d+(.\d+)*$/.test(this) && this != 0;
}
//验证非负小数
String.prototype.exp_isFloatN = function () {
return /^\d+(.\d+)*$/.test(this);
}
//验证负小数
String.prototype.exp_isFloatF = function () {
return /^-\d+(.\d+)*$/.test(this);
} //中文
String.prototype.exp_isCN = function () {
return /^[\u4e00-\u9fa5]+$/.test(this);
}
//英文
String.prototype.exp_isEN = function () {
return /^[a-zA-Z]+$/.test(this);
}
//数字
String.prototype.exp_isNum = function () {
return /^[0-9]+$/.test(this);
}
//中文英文数字
String.prototype.exp_isStr = function () {
return /^[\u4e00-\u9fa5a-zA-Z0-9]+$/.test(this);
}
//中文英文数字下划线横线
String.prototype.exp_isStr1 = function () {
return /^[\u4e00-\u9fa5a-zA-Z0-9\_\-]+$/.test(this);
} //手机号码(需更新)
String.prototype.exp_isPhone = function () {
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);
}
//电话 区号-电话-分机号 分机号可不写
String.prototype.exp_isTel = function () {
return /^[0][1-9][0-9]{1,2}[\-\*]?[1-9][0-9]{6,7}([-][0-9]+)*$/.test(this);
}
//电子邮箱
String.prototype.exp_isEmail = function () {
return /^[a-zA-Z0-9\_\.]+@[a-zA-Z0-9]+\.[a-zA-Z]+(\.[a-zA-Z]+)*$/.test(this);
} //长度 min最小长度(包含) max最大长度(包含)
String.prototype.exp_isLength = function (min, max) {
var valMin = min == null ? true : this.length > min - 1;
var valMax = max == null ? true : this.length < max + 1;
return valMin && valMax;
}
//去除前后空格
String.prototype.exp_Trim = function () {
return this.replace(/(^\s*)|(\s*$)/g, "");
}
//去除前空格
String.prototype.exp_TrimL = function () {
return this.replace(/^\s/g, "");
}
//去除后空格
String.prototype.exp_TrimR = function () {
return this.replace(/\s*$/g, "");
}
//去除所有空格
String.prototype.exp_TrimA = function () {
return this.replace(/\s/g, "");
}
//密码强度 0空/1弱/2中/3强
String.prototype.exp_PwdLevel = function (pwd) {
var num = pwd.replace(/[^0-9]/g, "").length;/*数字个数*/
var en = pwd.replace(/[^a-zA-Z]/g, "").length;/*字母个数*/
var punctuation = pwd.replace(/[0-9a-zA-Z]/g, "").length;/*标点个数*/
return (num > 0 ? 1 : 0) + (en > 0 ? 1 : 0) + (punctuation > 0 ? 1 : 0)
}
//获取地址栏值 null
String.prototype.exp_GetQueryString =function GetQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return null;
}

exp_validate.js

function

 /// <reference path="/Scripts/expand-fn/exp.validate.js" />
/*数据验证 liuph 2015-03-03
直接使用全局变量expV.方法
*/
var expV = {};
//验证整数
expV.isInt = function (str) {
return /^[+-]?\d+$/.test(str);
}
//验证正整数
expV.isIntT = function (str) {
return /^\d+$/.test(str) && str != 0;
}
//验证非负整数
expV.isIntN = function (str) {
return /^\d+$/.test(str);
}
//验证负整数
expV.isIntF = function (str) {
return /^-\d+$/.test(str);
}
//验证小数
expV.isFloat = function (str) {
return /^[+-]?\d+(.\d+)*$/.test(str);
}
//验证正小数
expV.isFloatT = function (str) {
return /^\d+(.\d+)*$/.test(str) && str != 0;
}
//验证非负小数
expV.isFloatN = function (str) {
return /^\d+(.\d+)*$/.test(str);
}
//验证负小数
expV.isFloatF = function (str) {
return /^-\d+(.\d+)*$/.test(str);
} //中文
expV.isCN = function (str) {
return /^[\u4e00-\u9fa5]+$/.test(str);
}
//英文
expV.isEN = function (str) {
return /^[a-zA-Z]+$/.test(str);
}
//数字
expV.isNum = function (str) {
return /^[0-9]+$/.test(str);
}
//中文英文数字
expV.isStr = function (str) {
return /^[\u4e00-\u9fa5a-zA-Z0-9]+$/.test(str);
}
//中文英文数字下划线横线
expV.isStr1 = function (str) {
return /^[\u4e00-\u9fa5a-zA-Z0-9\_\-]+$/.test(str);
} //手机号码(需更新)
expV.isPhone = function (str) {
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);
}
//仅作宽松验证
expV.isPhone1 = function (str) {
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);
}
//电话 区号-电话-分机号 分割用-或* 分机号可不写
expV.isTel = function (str) {
return /^[0][1-9][0-9]{1,2}[\-\*]?[1-9][0-9]{6,7}([-][0-9]+)*$/.test(str);
}
//电子邮箱
expV.isEmail = function (str) {
return /^[a-zA-Z0-9\_\.]+@[a-zA-Z0-9]+\.[a-zA-Z]+(\.[a-zA-Z]+)*$/.test(str);
} //长度 min最小长度(包含) max最大长度(包含)
expV.isLength = function (str, min, max) {
var valMinmin = (min == null ? true : str.length > min - 1);
var valMaxmax = (max == null ? true : str.length < max + 1);
return valMin && valMax;
}
//去除前后空格
expV.Trim = function (str) {
return str.replace(/(^\s*)|(\s*$)/g, "");
}
//去除前空格
expV.TrimL = function (str) {
return str.replace(/^\s/g, "");
}
//去除后空格
expV.TrimR = function (str) {
return str.replace(/\s*$/g, "");
}
//去除所有空格
expV.TrimA = function (str) {
return str.replace(/\s/g, "");
}
//密码强度 0空/1弱/2中/3强
expV.PwdLevel = function PwdLevel(pwd) {
var num = pwd.replace(/[^0-9]/g, "").length;/*数字个数*/
var en = pwd.replace(/[^a-zA-Z]/g, "").length;/*字母个数*/
var punctuation = pwd.replace(/[0-9a-zA-Z]/g, "").length;/*标点个数*/
return (num > 0 ? 1 : 0) + (en > 0 ? 1 : 0) + (punctuation > 0 ? 1 : 0)
}
//获取地址栏值 null
expV.GetQueryString = function GetQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return null;
}

exp.validate.js

exp.validate.js的更多相关文章

  1. 插件—jquery.validate.js

    前言 在学习jquery.validate.js中的一个小案例,只是这个插件的简单使用. 案例代码如下: <head>    <title></title>    ...

  2. 修改 jquery.validate.js 支持非form标签

    尝试使用markdown来写一篇blog,啦啦啦 源代码传送门:github 在特殊情况下我们使用jquery.validate.js对用户输入的内容做验证的时候,表单并不是一定包含在form之中,有 ...

  3. 表单验证插件之jquery.validate.js

    提到表单验证的插件,第一个想到的就是jquery.validate.js,所以小生想在这里稍微详细地说一下这款插件的具体使用方法,便于理解,我直接附上整段demo的代码(没怎么调样式,主要是看js): ...

  4. Jquery客户端校验——jquery.validate.js

    jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求.该插件捆绑了一套有用的验证方法,包括 URL 和电子邮件验证 ...

  5. MVC validate.js下使用 ajaxSubmit

    首页定义验证实体 using System.ComponentModel.DataAnnotations; using System.Web.Mvc; namespace MvcApplication ...

  6. jQuery验证控件jquery.validate.js使用说明

    官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuery plugin: Validation 使用说明 转载 ...

  7. jquery.validate.js插件使用

    jQuery验证控件jquery.validate.js使用说明+中文API 官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-valid ...

  8. jquery.validate.js表单验证

    一.用前必备官方网站:http://bassistance.de/jquery-plugins/jquery-plugin-validation/ API: http://jquery.bassist ...

  9. jQuery验证控件jquery.validate.js使用说明+中文API

    官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuery plugin: Validation 使用说明 学习 ...

随机推荐

  1. 学习SQLAlchemy Core

    有时间了就要慢慢看,死守DJANGO ORM,明显没有SQLAlchemy有优势. 因为SQLAlchemy针对整个PYTHON都是有用的. 找了本书,慢慢撸. <Essential.SQLAl ...

  2. <转>FreeMarker内置函数

    一. Sequence的内置函数1. sequence?first 返回sequence的第一个值.2. sequence?last 返回sequence的最后一个值.3. sequence?reve ...

  3. php开发(TP框架使用)

    由于最近玩了PHP,我向来有个原则,学一门服务端语言至少得玩两个框架,前段时间用PHP写了些demo+小项目,看见身边有人在使用TP,于是乎鼓捣学习学习.如何学,无非也就是做个小demo:就目前看来现 ...

  4. 无法打开包括文件:“windows.h”: No such file or directory

      VS2012 出现如下错误: 无法打开包括文件:"windows.h": No such file or directory   解决办法,将 C:\Program Files ...

  5. 在Salesforce中实现对Object的增删改查操作

    1): Insert Statement    http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=ap ...

  6. 使用python做科学计算

    这里总结一个guide,主要针对刚开始做数据挖掘和数据分析的同学 说道统计分析工具你一定想到像excel,spss,sas,matlab以及R语言.R语言是这里面比较火的,它的强项是强大的绘图功能以及 ...

  7. canvas加载进度条

    <!DOCTYPE html> <html><head><meta http-equiv="Content-Type" content=& ...

  8. Jmeter 小攻略(转)

    http://www.myexception.cn/open-source/1346307.html

  9. thinkphp几个表的数据合并,并用数组分页

    控制器: //金币扣除 public function jbkc(){ $map['UG_dataType']= 'xtkc'; $list1 = M ( 'userget' )->where ...

  10. 各浏览器抗uaf机制

    今年中旬,微软针对旗下ie浏览器中大量出现的uaf漏洞,对ie浏览器的安全机制进行了一个大幅度的升级,其中主要体现为隔离堆及延迟释放两个机制,顿时又将uaf漏洞的利用向上提升了一个大坎, 但是类似的对 ...