gistfile1.txt \t 制表符,其实就是一个“Tab”键\r 回车符\n 换行符在Unix为扩展的系统,在每行的结尾只有"\n",而在window下则是:"\r\n"(顺序不能换),为了解决系统的差异,就出现了两种: \r ||
\t 制表符,其实就是一个“Tab”键
\r 回车符
\n 换行符
在Unix为扩展的系统,在每行的结尾只有"\n",而在window下则是:"\r\n"(顺序不能换),为了解决系统的差异,就出现了两种: \r || \n. 所以一般,我们匹配换行需要使用.\r||\n一起使用.
var reg = /[\r\n]/g;
. 匹配换行符以外的任意字符
\d 匹配所有数字
\D 匹配非数字
\s 匹配一个空格符
\S 匹配非空格
\w 匹配字母数字下划线=>其实就是匹配单词word(简单易懂)
\W 匹配!字母数字下划线=>就是不匹配单词
^ 匹配字符串的开头,在多行检索中,匹配一行的开头
$ 匹配字符串的结尾,在多行检索中,匹配一行的结尾
\b 匹配一个单词的边界
\B 匹配非单词边界
* 重复零次或更多次
+ 重复一次或更多次
? 重复零次或一次
{n} 重复n次
{n,} 重复n次或更多次
{n, m} 重复n到m次
var ENTER_MSG = "\n";
var NOT_NULL = "不能为空,请修改!";
var LENGTH_EQUAL_MSG = "的最大长度为";
var MODIFY_MSG = "请修改!";
var COMMA_MSG = ",";
var ILLEGAL_REGEX = "不符合输入格式,请修改!";
/*
* 检查输入项是否为空 不向用户提示信息
*
* 入参为 ID值 如"#companyName"
*
* 例子:isEmpty(“#companyName”)
*
* Return false --不为空
* true -- 为空
*
*/
function isEmpty(id){
var value = $.trim($(id).val());
if (value == null || value.length == 0)
return true;
else
return false;
}
/*
* 检查下拉框是否为空,向用户发出提示
*
* 入参为 ID值 如"#companyName"
*
* 例:check_select_empty('#companyName','公司名称');
*
* Return false --不为空
* true -- 为空
*
*/
function check_select_empty(id,msg){
var value=$.trim($(id).val());
if(value == null || value == -1 || value == ""){
toastr.error(msg + NOT_NULL,"提示消息");
return false;
}
return true;
}
/*
* 函数功能:检查非空,向用户发出提示
*
* Parameter id -- 输入项的ID值
* msg -- 提示信息
* 例:check_empty('#companyName','公司名称');
*
* return --false 为空 true 不为空
*/
function check_empty(id,msg){
if(isEmpty(id)){
toastr.error(msg + NOT_NULL,"提示消息");
return false;
}
return true;
}
/*
* 函数功能:获取输入值的长度
*
* Parameter value --输入项的值
*
* return --返回输入项的实际长度 在 Unicode 字符中,因为汉字的编码都是大于255,所以只要编码大于255的都是汉字 一个汉字占2个字符
*/
function getValueLen(value){
var len=0;
for(var i=0;i
255){
len=len+2;
}
else{
len=len+1;
}
}
return len;
}
/*
* 函数功能:检查长度是否等于固定长度,向用户发出提示
*
* Parameter id -- 输入项的ID;
* length -- 要求的长度;
* msg -- 提示信息;
*
* Return false --不等于
* true -- 等于
*
* 例:check_length('#companyName','40','公司名称');
*/
function check_length(id,length,msg){
var value=$.trim($(id).val());
var count=getValueLen(value);
if(count>length){
toastr.error(msg + LENGTH_EQUAL_MSG + length + COMMA_MSG + MODIFY_MSG,"提示消息");
return false;
}
return true;
}
/*
* 函数功能:验证日期格式 先以 (yyyy-MM-dd)格式为准 根据需要再调整
*
* Parameter id -- 输入项的ID;
* length -- 要求的长度;
* msg -- 提示信息;
*
* Return false --格式不正确
* true -- 格式正确
*
* 例:check_date('#birthday');
*/
function check_date(id,msg){
var value=$.trim($(id).val());
var pattern = /^(\d{4}-\d{2}-\d{2})$/;
if(!pattern.test(value)){
toastr.error(msg+"请输入格式为yyyy-MM-dd的日期","提示消息");
return false;
}
else{
return true;
}
}
/*
* 函数功能:验证日期格式 先以 (yyyy-MM)格式为准 根据需要再调整
*
* Parameter id -- 输入项的ID;
* length -- 要求的长度;
* msg -- 提示信息;
*
* Return false --格式不正确
* true -- 格式正确
*
* 例:check_date('#birthday');
*/
function check_yearmon(id,msg){
var value=$.trim($(id).val());
var pattern = /^(\d{4}-\d{2})$/;
if(!pattern.test(value)){
toastr.error(msg+"请输入格式为yyyy-MM的日期","提示消息");
return false;
}
else{
return true;
}
}
/*
* 函数功能:转换输入转换为日期
*
* Parameter value --要转换的数据项;
*
*
*
* Return data 转换为数字的日期
*
* 例:check_date('#birthday');
*/
function tranferDateToNumber(value){
var arrDate = value.split("-");
if(parseInt(arrDate[0],10) < 100)
arrDate[0] = 2000 + parseInt(arrDate[0],10) + "";
var newdate = new Date(arrDate[0],(parseInt(arrDate[1],10) -1)+"",arrDate[2]);
return newdate;
}
/*
* 函数功能:检查是否匹配正则表达式,向用户发出提示
*
* Parameter id -- 输入项的ID;
* msg -- 提示信息;
* pattern -- 正则表达式
*
* Return false --不合法
* true -- 合法
*
* 例:regex_match_msg('#mobile','手机号','A-Z');
*/
function regex_match_msg(id,msg,pattern)
{
var regex = new RegExp(pattern);
var inputvalue=$.trim($(id).val());
if (regex.test(inputvalue))
return true;
toastr.error(msg +ILLEGAL_REGEX,"提示消息");
return false;
}
/*
* 函数功能:验证手机号
*
* Parameter id -- 输入项的ID;
* length -- 要求的长度;
* msg -- 提示信息;
*
* Return false --格式不正确
* true -- 格式正确
*
* 例:check_mobile('#mobile');
*/
function check_mobile(id,msg){
return regex_match_msg(id,msg,"^(1[34578]\\d{9})$");
}
/*
* 函数功能:检查EMAIL
*
* Parameter id -- 表单域文本框的英文名;
* msg -- 表单域文本框的中文标识为用户提供提示信息;
*
* Return false -- 不符合要求格式
* true -- 符合要求格式
*
* 例:check_email("#email","电子邮箱");
*
*
*/
function check_email(id,msg){
return regex_match_msg(id,msg,"^(\\S+@\\S+)?$");
}
/*
* 函数功能:检查企业组织机构代码 如 qw90iu871-9
*
* Parameter id -- 表单域文本框的英文名;
* msg -- 表单域文本框的中文标识为用户提供提示信息;
*
* Return false -- 不符合要求格式
* true -- 符合要求格式
*
* 例:check_orgCode("#orgCode","组织机构代码");
*
*
*/
function check_orgCode(id,msg){
return regex_match_msg(id,msg,"^([0-9A-Z]){8}-[0-9|X]$");
}
/*
* 函数功能:检查字符串是否为数字、加号、空格、横线,向用户发出提示
*
* Parameter ID -- 输入项的ID;
* msg -- 表单域文本框的中文标识为用户提供提示信息;
*
* Return false --包含数字、加号、空格、横线以外的字符
* true -- 不包含
*
* 例:check_phone('#telephone','固定电话');
*
*
*/
function check_phone(id,msg)
{
return regex_match_msg(id,msg,"[ 0-9-///+]*$");
}
/**
* 验证银行卡号(目前只验证银行卡必须为数字并且不超过数据库支持的长度)
*/
function check_bankCardNo(id,msg){
return regex_match_msg(id,msg,"^[0-9]{12,30}$");
}
/**
* 验证个人身份证
*/
function check_idNum(id,msg){
return regex_match_msg(id,msg,"^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|(X|x))$");
}
/**验证年份
*
* @param year
*/
function check_year(year,msg){
var regex = new RegExp("^[1-9][0-9]{3}$");
if (regex.test(year))
return true;
toastr.error(msg +ILLEGAL_REGEX,"提示消息");
return false;
}
/**
* 验证月份
* @param month
* @returns {Boolean}
*/
function check_month(month,msg){
var regex = new RegExp("^(0[1-9]|1[0-2])$");
if (regex.test(month))
return true;
toastr.error(msg +ILLEGAL_REGEX,"提示消息");
return false;
}
/**
* 验证日
* @param day
*/
function check_day(day,msg){
var regex = new RegExp("^(0[1-9]|1[0-9]|2[0-9]|3[0-1])$");
if (regex.test(day))
return true;
toastr.error(msg +ILLEGAL_REGEX,"提示消息");
return false;
}
/**
* 验证金额相关格式
* @param id
* @param msg
*/
function check_amount(id,msg){
return regex_match_msg(id,msg,"^[0-9]+\.[0-9]+$");
}
/**
* 验证是否是数字
* @param id
* @param msg
*/
function check_number(id,msg){
return regex_match_msg(id,msg,"^[1-9]$");
}
/**
* 验证保险产品元素的顺序是否1-99
* @param id
* @param msg
*/
function check_InsElementSeq(id,msg){
return regex_match_msg(id,msg,"^([1-9]\\d|\\d|-1)$");
}
/**
* 验证金额格式(可以是整数也可以是小数并且可以是整数加小数)
* @param id
* @param msg
*/
function check_amount1(id,msg){
return regex_match_msg(id,msg,"(^[0-9]+$)|(^[0-9]+\.[0-9]+$)");
}
/**
* 验证含有中文字符的长度(是为了兼容oralce数据库)
* racle数据库为UTF-8编码格式,采用3个byte
* @param str
* @returns {Number}
*/
function getStrRealSize(str){
var realLength = 0, len = str.length, charCode = -1;
for (var i = 0; i < len; i++) {
charCode = str.charCodeAt(i);
if (charCode >= 0 && charCode <= 128) realLength += 1;
else realLength += 3;
}
return realLength;
}
/**
* 验证oracle中数据的长度是否超出实际的长度
* @param str
* @param limitLength
* @returns {Boolean}
*/
function validateStrByLength(str,limitLength){
if(getStrRealSize(str) > limitLength){
return false;
}else{
return true;
}
}
/**
* 验证固定电话
* @param id
* @param msg
*/
function checkIsTel(id, msg) {
var inputvalue=$.trim($(id).val());
var patrn = /^((0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;
if (patrn.test(inputvalue)){
return true
}
toastr.error(msg +ILLEGAL_REGEX,"提示消息");
return false
}
