JS常用的正则表达式包
结构:
Code:
/*
用途:检查输入的Email信箱格式是否正确
输入:strEmail:字符串
返回:如果通过验证返回true,否则返回false
*/
function checkEmail(strEmail)
{
var emailReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
if ( emailReg.test(strEmail) ) {
return true;
}
else {
// alert("您输入的Email地址格式不正确!");
return false;
}
};
/*
用途:校验ip地址的格式
输入:strIP:ip地址
返回:如果通过验证返回true,否则返回false;
*/
function isIP(strIP)
{
if (isNull(strIP)) {
return false;
}
var re = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/ //匹配IP地址的正则表达式
if (re.test(strIP)) {
if ( RegExp.$1 < 256 && RegExp.$2 < 256 && RegExp.$3 < 256 && RegExp.$4 < 256) {
return true;
}
}
return false;
};
/*
用途:检查输入手机号码是否正确
输入:strMobile:字符串
返回:如果通过验证返回true,否则返回false
*/
function checkMobile( strMobile )
{ //13588888888
var regu = /^[1][345678][0-9]{9}$/;
var re = new RegExp(regu);
if (re.test(strMobile)) {
return true;
}
else {
return false;
}
};
/*
用途:检查输入的电话号码格式是否正确
输入:strPhone:字符串
返回:如果通过验证返回true,否则返回false
*/
function checkPhone( strPhone )
{
var phoneRegWithArea = /^[0][1-9]{2,3}-[0-9]{5,10}$/;
var phoneRegNoArea = /^[1-9]{1}[0-9]{5,8}$/;
var prompt = "您输入的电话号码不正确!"
if ( strPhone.length > 9 ) {
if ( phoneRegWithArea.test(strPhone) ) {
return true;
}
else {
alert( prompt );
return false;
}
}
else {
if ( phoneRegNoArea.test( strPhone ) ) {
return true;
}
else {
alert( prompt );
return false;
}
}
};
/*
用途:检查输入字符串是否为空或者全部都是空格
输入:str
返回:如果全是空返回true,否则返回false
*/
function isNull( str )
{
if ( str == "" ) {
return true;
}
var regu = "^[ ]+$";
var re = new RegExp(regu);
return re.test(str);
};
/*
用途:检查输入对象的值是否符合整数格式
输入:str 输入的字符串
返回:如果通过验证返回true,否则返回false
*/
function isInteger( str )
{
var regu = /^[-]{0,1}[0-9]{1,}$/;
return regu.test(str);
};
/*
用途:检查输入字符串是否符合正整数格式
输入:s:字符串
返回:如果通过验证返回true,否则返回false
*/
function isNumber( s )
{
var regu = "^[0-9]+$";
var re = new RegExp(regu);
if (s.search(re) != - 1) {
return true;
}
else {
return false;
}
};
/*
用途:检查输入字符串是否是带小数的数字格式,可以是负数
输入:str:字符串
返回:如果通过验证返回true,否则返回false
*/
function isDecimal( str )
{
if (isInteger(str)) {
return true;
}
var re = /^[-]{0,1}(\d+)[\.]+(\d+)$/;
if (re.test(str)) {
if (RegExp.$1 == 0 && RegExp.$2 == 0) {
return false;
}
return true;
}
else {
return false;
}
};
/*
用途:检查输入对象的值是否符合端口号格式
输入:str 输入的字符串
返回:如果通过验证返回true,否则返回false
*/
function isPort( str )
{
return (isNumber(str) && str < 65536);
};
/*
用途:检查输入字符串是否符合金额格式,格式定义为带小数的正数,小数点后最多三位
输入:s:字符串
返回:如果通过验证返回true,否则返回false
*/
function isMoney( s )
{
var regu = "^[0-9]+[\.][0-9]{0,3}$";
var re = new RegExp(regu);
if (re.test(s)) {
return true;
}
else {
return false;
}
};
/*
用途:检查输入字符串是否只由英文字母和数字和下划线组成
输入:s:字符串
返回:如果通过验证返回true,否则返回false
*/
function isNumberOr_Letter( s )
{
//判断是否是数字或字母
var regu = "^[0-9a-zA-Z\_]+$";
var re = new RegExp(regu);
if (re.test(s)) {
return true;
}
else {
return false;
}
};
/*
用途:检查输入字符串是否只由英文字母和数字组成
输入:s:字符串
返回:如果通过验证返回true,否则返回false
*/
function isNumberOrLetter( s )
{
//判断是否是数字或字母
var regu = "^[0-9a-zA-Z]+$";
var re = new RegExp(regu);
if (re.test(s)) {
return true;
}
else {
return false;
}
};
/*
用途:检查输入字符串是否只由汉字、字母、数字组成
输入:s:字符串
返回:如果通过验证返回true,否则返回false
*/
function isChinaOrNumbOrLett( s )
{
//判断是否是汉字、字母、数字组成
var regu = "^[0-9a-zA-Z\u4e00-\u9fa5]+$";
var re = new RegExp(regu);
if (re.test(s)) {
return true;
}
else {
return false;
}
};
/*
用途:判断是否是日期
输入:date:日期;fmt:日期格式
返回:如果通过验证返回true,否则返回false
*/
function isDate( date, fmt )
{
if (fmt == null) {
fmt = "yyyyMMdd";
}
var yIndex = fmt.indexOf("yyyy");
if (yIndex ==- 1) {
return false;
}
var year = date.substring(yIndex, yIndex + 4);
var mIndex = fmt.indexOf("MM");
if (mIndex ==- 1) {
return false;
}
var month = date.substring(mIndex, mIndex + 2);
var dIndex = fmt.indexOf("dd");
if (dIndex ==- 1) {
return false;
}
var day = date.substring(dIndex, dIndex + 2);
if (!isNumber(year) || year > "2100" || year < "1900") {
return false;
}
if (!isNumber(month) || month > "12" || month < "01") {
return false;
}
if (day > getMaxDay(year, month) || day < "01") {
return false;
}
return true;
};
function getMaxDay(year, month)
{
if (month == 4 || month == 6 || month == 9 || month == 11) {
return "30";
}
if (month == 2) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return "29";
}
else {
return "28";
}
return "31";;
}
};
/*
用途:字符1是否以字符串2结束
输入:str1:字符串;str2:被包含的字符串
返回:如果通过验证返回true,否则返回false
*/
function isLastMatch(str1, str2)
{
var index = str1.lastIndexOf(str2);
if (str1.length == index + str2.length) {
return true;
}
return false;
};
/*
用途:字符1是否以字符串2开始
输入:str1:字符串;str2:被包含的字符串
返回:如果通过验证返回true,否则返回false
*/
function isFirstMatch(str1, str2)
{
var index = str1.indexOf(str2);
if (index == 0) {
return true;
}
return false;
};
/*
用途:字符1是包含字符串2
输入:str1:字符串;str2:被包含的字符串
返回:如果通过验证返回true,否则返回false
*/
function isMatch(str1, str2)
{
var index = str1.indexOf(str2);
if (index ==- 1) {
return false;
}
return true;
};
/*
用途:检查输入的起止日期是否正确,规则为两个日期的格式正确,且结束如期>=起始日期
输入:startDate:起始日期,字符串; endDate:结束如期,字符串
返回:如果通过验证返回true,否则返回false
*/
function checkTwoDate( startDate, endDate )
{
if ( !isDate(startDate) ) {
alert("起始日期不正确!");
return false;
}
else if ( !isDate(endDate) ) {
alert("终止日期不正确!");
return false;
}
else if ( startDate > endDate ) {
alert("起始日期不能大于终止日期!");
return false;
}
return true;
};
/*
用途:检查复选框被选中的数目
输入:checkboxID:字符串
返回:返回该复选框中被选中的数目
*/
function checkSelect( checkboxID )
{
var check = 0;
var i = 0;
if ( document.all(checkboxID).length > 0 )
{
for ( i = 0; i < document.all(checkboxID).length; i++ ) {
if ( document.all(checkboxID).item( i ).checked ) {
check += 1;
}
}
}
else {
if ( document.all(checkboxID).checked ) {
check = 1;
}
}
return check;
}
function getTotalBytes(varField)
{
if (varField == null) {
return - 1;
}
var totalCount = 0;
for (i = 0; i < varField.value.length; i++) {
if (varField.value.charCodeAt(i) > 127) {
totalCount += 2;
}
else {
totalCount++ ;
}
}
return totalCount;
}
function getFirstSelectedValue( checkboxID )
{
var value = null;
var i = 0;
if ( document.all(checkboxID).length > 0 )
{
for ( i = 0; i < document.all(checkboxID).length; i++ )
{
if ( document.all(checkboxID).item( i ).checked ) {
value = document.all(checkboxID).item(i).value;
break;
}
}
}
else {
if ( document.all(checkboxID).checked ) {
value = document.all(checkboxID).value;
}
}
return value;
}
function getFirstSelectedIndex( checkboxID )
{
var value = - 2;
var i = 0;
if ( document.all(checkboxID).length > 0 )
{
for ( i = 0; i < document.all(checkboxID).length; i++ ) {
if ( document.all(checkboxID).item( i ).checked ) {
value = i;
break;
}
}
}
else {
if ( document.all(checkboxID).checked ) {
value = - 1;
}
}
return value;
}
function selectAll( checkboxID, status )
{
if ( document.all(checkboxID) == null) {
return;
}
if ( document.all(checkboxID).length > 0 )
{
for ( i = 0; i < document.all(checkboxID).length; i++ ) {
document.all(checkboxID).item( i ).checked = status;
}
}
else {
document.all(checkboxID).checked = status;
}
}
function selectInverse( checkboxID )
{
if ( document.all(checkboxID) == null) {
return;
}
if ( document.all(checkboxID).length > 0 )
{
for ( i = 0; i < document.all(checkboxID).length; i++ )
{
document.all(checkboxID).item( i ).checked = !document.all(checkboxID).item( i ).checked;
}
}
else {
document.all(checkboxID).checked = !document.all(checkboxID).checked;
}
}
function checkDate( value )
{
if (value == '') {
return true;
}
if (value.length != 8 || !isNumber(value)) {
return false;
}
var year = value.substring(0, 4);
if (year > "2100" || year < "1900") {
return false;
}
var month = value.substring(4, 6);
if (month > "12" || month < "01") {
return false;
}
var day = value.substring(6, 8);
if (day > getMaxDay(year, month) || day < "01") {
return false;
}
return true;
};
/*
用途:检查输入的起止日期是否正确,规则为两个日期的格式正确或都为空且结束日期>=起始日期
输入:startDate:起始日期,字符串; endDate: 结束日期,字符串
返回:如果通过验证返回true,否则返回false
*/
function checkPeriod( startDate, endDate )
{
if ( !checkDate(startDate) ) {
alert("起始日期不正确!");
return false;
}
else if ( !checkDate(endDate) ) {
alert("终止日期不正确!");
return false;
}
else if ( startDate > endDate ) {
alert("起始日期不能大于终止日期!");
return false;
}
return true;
};
/*
用途:检查证券代码是否正确
输入:secCode:证券代码
返回:如果通过验证返回true,否则返回false
*/
function checkSecCode( secCode )
{
if ( secCode.length != 6 ) {
alert("证券代码长度应该为6位");
return false;
}
if (!isNumber( secCode ) ) {
alert("证券代码只能包含数字");
return false;
}
return true;
};
/*
function:cTrim(sInputString,iType)
description:字符串去空格的函数
parameters:iType:1=去掉字符串左边的空格;2=去掉字符串左边的空格;0=去掉字符串左边和右边的空格
return value:去掉空格的字符串
*/
function cTrim(sInputString, iType)
{
var sTmpStr = ' ';
var i = - 1;
if (iType == 0 || iType == 1)
{
while (sTmpStr == ' ') {
++i;
sTmpStr = sInputString.substr(i, 1);
}
sInputString = sInputString.substring(i);
}
if (iType == 0 || iType == 2)
{
sTmpStr = ' ';
i = sInputString.length;
while (sTmpStr == ' ') {
--i;
sTmpStr = sInputString.substr(i, 1);
}
sInputString = sInputString.substring(0, i + 1);
}
return sInputString;
};
如何在其他文件中引入这个包?
我想大家应该都会吧,为了全面 ,补一下
JS常用的正则表达式包的更多相关文章
- Node.js 常用工具util包
Node.js 常用工具 util 是一个Node.js 核心模块,提供常用函数的集合,用于弥补核心JavaScript 的功能 过于精简的不足. util.isError(obj); util.is ...
- php 或js 常用的正则表达式
1. 平时做网站经常要用正则表达式,下面是一些讲解和例子,仅供大家参考和修改使用:2. "^\d+$" //非负整数(正整数 + 0)3. "^[0-9 ...
- js 常用的正则表达式
以下收录一些我经常用到的正则表达式,因为工作场景中用到正则的地方几乎都跟validate插件的验证有关系, 所以以下正则也是$.validator.addMethod() 的拓展: validate: ...
- JS常用各种正则表达式
1.非负整数 /^\d+$/ 2.正整数 /^[0-9]*[1-9][0-9]*$/ 3.非正整数 /^((-\d+)|(0+))$/ 4.负整数 ...
- js常用的正则表达式
一.校验数字的表达式 数字:^[0-9]*$ n位的数字:^\d{n}$ 至少n位的数字:^\d{n,}$ m-n位的数字:^\d{m,n}$ 零和非零开头的数字:^(0|[1-9][0-9]*)$ ...
- JS常用各种正则表达式(汇总)
匹配URL 这个url的正则表达式判断的JavaScript!比较全面的.它验证的情况包括IP,域名(domain),ftp,二级域名,域名中的文件,域名加上端口!用户名等等信息, function ...
- JS常用正则表达式备忘录
摘要: 玩转正则表达式. 原文:JS常用正则表达式备忘录 作者:前端小智 Fundebug经授权转载,版权归原作者所有. 正则表达式或"regex"用于匹配字符串的各个部分 下面是 ...
- js常用正则表达式,滚蛋吧!你们测试组bug,让你挑
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- js中常用的正则表达式
我一般对正则的使用方式如下,该方法会返回一个boolean值,然后对这个返回值来进行判断 // 判断是否是整数 function isInt(num) { var reg = new RegExp(& ...
随机推荐
- Vue + Webpack 根据不同环境打包
修改 prod.env.js // 当前正在运行的脚本名称 const TARGET = process.env.npm_lifecycle_event // 第一个参数 let argv = pro ...
- HTML学习第六天
HTML学习第六天 一.全局属性 contentEditable属性,控制标签元素的可修改性,默认与“”(空字符串)都代表真,即可编辑 <!DOCTYPE html> <html l ...
- Oracle修改密码
1. 登陆oracle sqlplus '/as sysdba' 2. 修改密码 ALTER USER 用户名IDENTIFIED BY 要修改的密码 ; 3.解锁 alter user 用户名 ac ...
- Flutter Web环境搭建
接上篇Flutter Windows下AndroidStudio环境搭建 1.https://github.com/flutter/flutter_web 下载放到本地路径下 2.系统Path增加(根 ...
- Day7 - J - Raising Modulo Numbers POJ - 1995
People are different. Some secretly read magazines full of interesting girls' pictures, others creat ...
- Codeforces Forethought Future Cup Elimination Round 选做
1146C Tree Diameter 题意 交互题.有一棵 \(n(n\le 100)\) 个点的树,你可以进行不超过 \(9\) 次询问,每次询问两个点集中两个不在同一点集的点的最大距离.求树的直 ...
- DCGAN增强图片数据集
DCGAN增强图片数据集 1.Dependencies Python 3.6+ PyTorch 0.4.0 numpy 1.14.1, matplotlib 2.2.2, scipy 1.1.0 im ...
- oracle 导出时报错EXP-00011:table不存在
oracle11g,在用exp命令备份数据库时,如果表中没有数据报EXP-00011错误,对应的表不存在.这导致对应的空表无法备份. 原因:11g默认创建一个表时不分配segment,只有在插入数据时 ...
- C语言备忘录——static
对于这个关键字我一直没有弄清楚,今天特地去花了一定的时间去理解这个关键字.在函数或变量声明时,在数据类型前加上 static 后会有以下几个效果 一.用于函数定义时: 1.函数的链接属性会被修改,从e ...
- 英语语法 - the + 形容词 的意义
1,表示一类人 (复数) the young 青年 the old 老年the poor 穷人 the rich 富人the sick 病人 The old need care more than ...