简单实用的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. SSH入门简单搭建例子

    因为公司涉及项目使用SSH,为了解SSH搭建方式和运作原理,就自己搭建了一个. 采用尽量以最少的JAR包,搭建一个简单的struts2+spring+hibernate环境,希望像我这样的入门者都能理 ...

  2. Linux 6.5網卡配置

    Linux 6.5 網卡配置 路徑:/etc/sysconfig/network-scripts 1.關閉NetworkManager服務 [root@rhcsasm2 network-scripts ...

  3. C# 非UI线程对控件的控制

    第一步:定义委托 public delegate void wei(string ss); 第二步:控制UI的方法 public void get1(string ss) { richTextBox1 ...

  4. apistore接口调用demo

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. WPF QuickStart系列之附加属性(Attached Property)

    这一篇博客是关于如何使用附加属性和创建自定义附加属性的. 1. 附加属性使用, WPF中对附加属性使用最多的莫过于对控件布局时设置控件的位置,例如在Canvas中有一个Rectangle, Ellip ...

  6. 微软改名部再次大显神威——ASP.NET 5改名ASP.NET Core 1.0

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:在计算机科学领域只有两件难事:缓存不可用和命名.--Phil Karlton 今天,S ...

  7. Debian 的 preinst, postinst, prerm, 和 postrm 脚本

    转自:http://jianjian.blog.51cto.com/35031/395468 这些是软件包安装前后自动运行的可执行脚本. 统称为控制文件, 是 Deian 软件包的"控制&q ...

  8. JVM字节码之整型入栈指令(iconst、bipush、sipush、ldc)

    官网:http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html 原文地址:http://www.linmuxi.com/2016/02 ...

  9. Pig用户自定义函数(UDF)转

    原文地址:http://blog.csdn.net/zythy/article/details/18326693 我们以气温统计和词频统计为例,讲解以下三种用户自定义函数. 用户自定义函数 什么时候需 ...

  10. MySQL中的SQL语言

    从功能上划分,SQL 语言可以分为DDL,DML和DCL三大类.1. DDL(Data Definition Language)数据定义语言,用于定义和管理 SQL 数据库中的所有对象的语言 :CRE ...