Regular expression instances commonly used in javascript
- 2020-03-30 03:00:34
- OfStack
This site before a lot of common JavaScript regular expression instances, are their own use, now put out to share with you.
function strlen(str)
{
var i;
var len;
len = 0;
for (i=0;i<str.length;i++)
{
if (str.charCodeAt(i)>255) len+=2; else len++;
}
return len;
}
function f_check_number(obj)
{
if (/^d+$/.test(obj.value))
{
return true;
}
else
{
f_alert(obj," Please enter a number ");
return false;
}
}
function f_check_naturalnumber(obj)
{
var s = obj.value;
if (/^[0-9]+$/.test( s ) && (s > 0))
{
return true;
}
else
{
f_alert(obj," Please enter a natural number ");
return false;
}
}
function f_check_integer(obj)
{
if (/^(+|-)?d+$/.test( obj.value ))
{
return true;
}
else
{
f_alert(obj," Please enter an integer ");
return false;
}
}
function f_check_float(obj)
{
if (/^(+|-)?d+($|.d+$)/.test( obj.value ))
{
return true;
}
else
{
f_alert(obj," Please enter real Numbers ");
return false;
}
}
function f_check_double(obj){
var numReg;
var value = obj.value;
var strValueTemp, strInt, strDec;
var dtype = obj.eos_datatype;
var pos_dtype = dtype.substring(dtype.indexOf("(")+1,dtype.indexOf(")")).split(",");
var len = pos_dtype[0], prec = pos_dtype[1];
try
{
numReg =/[-]/;
strValueTemp = value.replace(numReg, "");
numReg =/[+]/;
strValueTemp = strValueTemp.replace(numReg, "");
//Integer
if(prec==0){
numReg =/[.]/;
if(numReg.test(value) == true){
f_alert(obj, " The input must be of integer type ");
return false;
}
}
if(strValueTemp.indexOf(".") < 0 ){
if(strValueTemp.length >( len - prec)){
f_alert(obj, " Integer bits cannot be exceeded "+ (len - prec) +" position ");
return false;
}
}else{
strInt = strValueTemp.substr( 0, strValueTemp.indexOf(".") );
if(strInt.length >( len - prec)){
f_alert(obj, " Integer bits cannot be exceeded "+ (len - prec) +" position ");
return false;
}
strDec = strValueTemp.substr( (strValueTemp.indexOf(".")+1), strValueTemp.length );
if(strDec.length > prec){
f_alert(obj, " The decimal places cannot be exceeded "+ prec +" position ");
return false;
}
}
return true;
}catch(e){
alert("in f_check_double = " + e);
return false;
}
}
function f_check_interval(obj)
{
var value = parseFloat(obj.value);
var dtype = obj.eos_datatype;
var pos_dtype = dtype.substring(dtype.indexOf("(")+1,dtype.indexOf(")")).split(",");
var minLimit = pos_dtype[0];
var maxLimit = pos_dtype[1];
var minVal = parseFloat(pos_dtype[0]);
var maxVal = parseFloat(pos_dtype[1]);
if(isNaN(value))
{
f_alert(obj, " The value must be a number ");
return false;
}
if((isNaN(minVal) && (minLimit != "-")) || (isNaN(maxVal) && (maxLimit != "+")))
{
f_alert(obj, " The boundary value must be numeric or - , +");
return false;
}
if(minLimit == "-" && !isNaN(maxVal))
{
if(value > maxVal)
{
f_alert(obj, " Value cannot exceed " + maxVal);
return false;
}
}
if(!isNaN(minVal) && maxLimit == "+")
{
if(value < minVal)
{
f_alert(obj, " Value cannot be less than " + minVal);
return false;
}
}
if(!isNaN(minVal) && !isNaN(maxVal))
{
if(minVal > maxVal)
{
f_alert(obj, " The starting value " + minVal + " Cannot be greater than the termination value " + maxVal);
}else
{
if(!(value <= maxVal && value >= minVal))
{
f_alert(obj, " Value should be in " + minVal + " and " + maxVal + " between ");
return false;
}
}
}
return true;
}
function f_check_zh(obj){
if (/^[u4e00-u9fa5]+$/.test(obj.value)) {
return true;
}
f_alert(obj," Please enter Chinese characters ");
return false;
}
function f_check_lowercase(obj)
{
if (/^[a-z]+$/.test( obj.value ))
{
return true;
}
f_alert(obj," Please enter lowercase English letters ");
return false;
}
function f_check_uppercase(obj)
{
if (/^[A-Z]+$/.test( obj.value ))
{
return true;
}
f_alert(obj," Please enter capital English letters ");
return false;
}
function f_check_letter(obj)
{
if (/^[A-Za-z]+$/.test( obj.value ))
{
return true;
}
f_alert(obj," Please enter English letters ");
return false;
}
function f_check_ZhOrNumOrLett(obj){ //Determining whether a Chinese character, letter, or number is written & PI;
var regu = "^[0-9a-zA-Zu4e00-u9fa5]+$";
var re = new RegExp(regu);
if (re.test( obj.value )) {
return true;
}
f_alert(obj," Please enter Chinese characters, letters or Numbers ");
return false;
}
function f_check_IP(obj)
{
var re=/^(d+).(d+).(d+).(d+)$/; //A regular expression & NBSP; that matches IP addresses;
if(re.test( obj.value ))
{
if( RegExp.$1<=255 && RegExp.$1>=0
&&RegExp.$2<=255 && RegExp.$2>=0
&&RegExp.$3<=255 && RegExp.$3>=0
&&RegExp.$4<=255 && RegExp.$4>=0 )
{
return true;
}
}
f_alert(obj," Please enter a valid computer IP address ");
return false;
}
function f_check_port(obj)
{
if(!f_check_number(obj))
return false;
if(obj.value < 65536)
return true;
f_alert(obj," Please enter a valid computer IP Address port number ");
return false;
}
function f_check_URL(obj){
var myReg = /^((http:[/][/])?w+([.]w+|[/]w*)*)?$/;
if(myReg.test( obj.value )) return true;
f_alert(obj," Please enter a valid web address ");
return false;
}
function f_check_email(obj){
var myReg = /^([-_A-Za-z0-9.]+)@([_A-Za-z0-9]+.)+[A-Za-z0-9]{2,3}$/;
if(myReg.test( obj.value )) return true;
f_alert(obj," Please enter a valid email address ");
return false;
}
function f_check_mobile(obj){
var regu =/(^[1][3][0-9]{9}$)|(^0[1][3][0-9]{9}$)/;
var re = new RegExp(regu);
if (re.test( obj.value )) {
return true;
}
f_alert(obj," Please enter the correct mobile phone number ");
return false;
}
function f_check_phone(obj)
{
var regu =/(^([0][1-9]{2,3}[-])?d{3,8}(-d{1,6})?$)|(^([0][1-9]{2,3})d{3,8}((d{1,6}))?$)|(^d{3,8}$)/;
var re = new RegExp(regu);
if (re.test( obj.value )) {
return true;
}
f_alert(obj," Please enter the correct telephone number ");
return false;
}
function f_check_zipcode(obj)
{
if(!f_check_number(obj))
return false;
if(obj.value.length!=6)
{
f_alert(obj," Zip code length must be 6 position ");
return false;
}
return true;
}
function f_check_userID(obj)
{
var userID = obj.value;
if(userID.length > 20)
{
f_alert(obj,"ID Length cannot be greater than 20");
return false;
}
if(!isNaN(userID.charAt(0)))
{
f_alert(obj,"ID The first character cannot be a number ");
return false;
}
if(!/^w{1,20}$/.test(userID))
{
f_alert(obj,"ID It can only be composed of Numbers, letters, and underscores ");
return false;
}
return true;
}
function f_check_IDno(obj)
{
var aCity={11:" Beijing ",12:" tianjin ",13:" hebei ",14:" shanxi ",15:" Inner Mongolia ",21:" liaoning ",22:" Ji Lin ",23:" heilongjiang ",31:" Shanghai ",32:" jiangsu ",33:" zhejiang ",34:" anhui ",35:" fujian ",36:" jiangxi ",37:" shandong ",41:" henan ",42:" hubei ",43:" hunan ",44:" guangdong ",45:" guangxi ",46:" hainan ",50:" chongqing ",51:" sichuan ",52:" guizhou ",53:" yunnan ",54:" Tibet ",61:" shaanxi ",62:" gansu ",63:" qinghai ",64:" ningxia ",65:" xinjiang ",71:" Taiwan ",81:" Hong Kong ",82:" Macau ",91:" foreign "};
var iSum = 0;
var info = "";
var strIDno = obj.value;
var idCardLength = strIDno.length;
if(!/^d{17}(d|x)$/i.test(strIDno)&&!/^d{15}$/i.test(strIDno))
{
f_alert(obj," Illegal id number ");
return false;
}
//In the latter operation, x corresponds to the number 10, so that translates to a ;
strIDno = strIDno.replace(/x$/i,"a");
if(aCity[parseInt(strIDno.substr(0,2))]==null)
{
f_alert(obj," Illegal region ");
return false;
}
if (idCardLength==18)
{
sBirthday=strIDno.substr(6,4)+"-"+Number(strIDno.substr(10,2))+"-"+Number(strIDno.substr(12,2));
var d = new Date(sBirthday.replace(/-/g,"/"))
if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))
{
f_alert(obj," Illegal birthday ");
return false;
}
for(var i = 17;i>=0;i --)
iSum += (Math.pow(2,i) % 11) * parseInt(strIDno.charAt(17 - i),11);
if(iSum%11!=1)
{
f_alert(obj," Illegal id number ");
return false;
}
}
else if (idCardLength==15)
{
sBirthday = "19" + strIDno.substr(6,2) + "-" + Number(strIDno.substr(8,2)) + "-" + Number(strIDno.substr(10,2));
var d = new Date(sBirthday.replace(/-/g,"/"))
var dd = d.getFullYear().toString() + "-" + (d.getMonth()+1) + "-" + d.getDate();
if(sBirthday != dd)
{
f_alert(obj," Illegal birthday ");
return false;
}
}
return true;
}
function f_check_formatStr(obj)
{
var str = obj.value;
var dtype = obj.eos_datatype;
var regu = dtype.substring(dtype.indexOf("(")+1,dtype.indexOf(")")); //Specified regular expression & NBSP;
var re = new RegExp(regu);
if(re.test(str))
return true;
f_alert(obj , " Does not meet the specified regular expression requirements ");
return false;
}
/*
Function: determine if it is a date ( format :yyyy years MM month dd day ,yyyy-MM-dd,yyyy/MM/dd,yyyyMMdd)
Message: did not enter or entered the date format error!
Use: f_check_date(obj)
Returns: bool
*/
function f_check_date(obj)
{
var date = Trim(obj.value);
var dtype = obj.eos_datatype;
var format = dtype.substring(dtype.indexOf("(")+1,dtype.indexOf(")")); //Date format & NBSP;
var year,month,day,datePat,matchArray;
if(/^(y{4})(-|/)(M{1,2})2(d{1,2})$/.test(format))
datePat = /^(d{4})(-|/)(d{1,2})2(d{1,2})$/;
else if(/^(y{4})( years )(M{1,2})( month )(d{1,2})( day )$/.test(format))
datePat = /^(d{4}) years (d{1,2}) month (d{1,2}) day $/;
else if(format=="yyyyMMdd")
datePat = /^(d{4})(d{2})(d{2})$/;
else
{
f_alert(obj," Wrong date format ");
return false;
}
matchArray = date.match(datePat);
if(matchArray == null)
{
f_alert(obj," Incorrect date length , Or a date with a non-numeric symbol ");
return false;
}
if(/^(y{4})(-|/)(M{1,2})2(d{1,2})$/.test(format))
{
year = matchArray[1];
month = matchArray[3];
day = matchArray[4];
} else
{
year = matchArray[1];
month = matchArray[2];
day = matchArray[3];
}
if (month < 1 || month > 12)
{
f_alert(obj," Month should be 1 to 12 The integer ");
return false;
}
if (day < 1 || day > 31)
{
f_alert(obj," The days of each month should be 1 to 31 The integer ");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31)
{
f_alert(obj," The month does not exist 31 No. ");
return false;
}
if (month==2)
{
var isleap=(year % 4==0 && (year % 100 !=0 || year % 400==0));
if (day>29)
{
f_alert(obj,"2 Month up to 29 day ");
return false;
}
if ((day==29) && (!isleap))
{
f_alert(obj," A leap year 2 Month is 29 day ");
return false;
}
}
return true;
}
/*
Function: the format of the calibration is yyyy years MM month dd day HH when mm points ss seconds ,yyyy-MM-dd HH:mm:ss,yyyy/MM/dd HH:mm:ss,yyyyMMddHHmmss
Message: not entered or entered the time format error
Use: f_check_time(obj)
Returns: bool
*/
function f_check_time(obj)
{
var time = Trim(obj.value);
var dtype = obj.eos_datatype;
var format = dtype.substring(dtype.indexOf("(")+1,dtype.indexOf(")")); //Date format & NBSP;
var datePat,matchArray,year,month,day,hour,minute,second;
if(/^(y{4})(-|/)(M{1,2})2(d{1,2}) (HH:mm:ss)$/.test(format))
datePat = /^(d{4})(-|/)(d{1,2})2(d{1,2}) (d{1,2}):(d{1,2}):(d{1,2})$/;
else if(/^(y{4})( years )(M{1,2})( month )(d{1,2})( day )(HH when mm points ss seconds )$/.test(format))
datePat = /^(d{4}) years (d{1,2}) month (d{1,2}) day (d{1,2}) when (d{1,2}) points (d{1,2}) seconds $/;
else if(format == "yyyyMMddHHmmss")
datePat = /^(d{4})(d{2})(d{2})(d{2})(d{2})(d{2})$/;
else
{
f_alert(obj," Wrong date format ");
return false;
}
matchArray = time.match(datePat);
if(matchArray == null)
{
f_alert(obj," Incorrect date length , Or a date with a non-numeric symbol ");
return false;
}
if(/^(y{4})(-|/)(M{1,2})2(d{1,2}) (HH:mm:ss)$/.test(format))
{
year = matchArray[1];
month = matchArray[3];
day = matchArray[4];
hour = matchArray[5];
minute = matchArray[6];
second = matchArray[7];
} else
{
year = matchArray[1];
month = matchArray[2];
day = matchArray[3];
hour = matchArray[4];
minute = matchArray[5];
second = matchArray[6];
}
if (month < 1 || month > 12)
{
f_alert(obj," Month should be 1 to 12 The integer ");
return false;
}
if (day < 1 || day > 31)
{
f_alert(obj," The days of each month should be 1 to 31 The integer ");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31)
{
f_alert(obj," The month does not exist 31 No. ");
return false;
}
if (month==2)
{
var isleap=(year % 4==0 && (year % 100 !=0 || year % 400==0));
if (day>29)
{
f_alert(obj,"2 Month up to 29 day ");
return false;
}
if ((day==29) && (!isleap))
{
f_alert(obj," A leap year 2 Month is 29 day ");
return false;
}
}
if(hour<0 || hour>23)
{
f_alert(obj," The hour should be 0 to 23 The integer ");
return false;
}
if(minute<0 || minute>59)
{
f_alert(obj," Points should be 0 to 59 The integer ");
return false;
}
if(second<0 || second>59)
{
f_alert(obj," Second is supposed to be 0 to 59 The integer ");
return false;
}
return true;
}
function isVisible(obj){
var visAtt,disAtt;
try{
disAtt=obj.style.display;
visAtt=obj.style.visibility;
}catch(e){}
if(disAtt=="none" || visAtt=="hidden")
return false;
return true;
}
function checkPrVis(obj){
var pr=obj.parentNode;
do{
if(pr == undefined || pr == "undefined") return true;
else{
if(!isVisible(pr)) return false;
}
}while(pr=pr.parentNode);
return true;
}
function f_alert(obj,alertInfo)
{
var caption = obj.getAttribute("eos_displayname");
if(caption == null)
caption = "";
alert(caption + " : " + alertInfo + " ! ");
obj.select();
if(isVisible(obj) && checkPrVis(obj))
obj.focus();
}
function isnull(str)
{
var i;
if(str.length == 0)
return true;
for (i=0;i<str.length;i++)
{
if (str.charAt(i)!=' ')
return false;
}
return true;
}
function checkInput(object)
{
var image;
var i;
var length;
if(object.eos_maxsize + "" != "undefined") length = object.eos_maxsize;
else length = 0;
if (object.eos_isnull=="true" && isnull(object.value)) return true;
if(length != 0 && strlen(object.value) > parseInt(length)) {
f_alert(object, " Beyond the maximum length " + length);
return false;
}
else {
if (object.eos_datatype + "" != "undefined")
{
var dtype = object.eos_datatype;
var objName = object.name;
//If the type name is followed by a parenthesis, the string before the parenthesis is considered the check type & PI;
if(dtype.indexOf("(") != -1)
dtype = dtype.substring(0,dtype.indexOf("("));
//Check & NBSP according to the check type of the page element;
try{
if(eval("f_check_" + dtype + "(object)") != true)
return false;
}catch(e){return true;}
if(objName.substring((objName.length-3),objName.length)=="min")
{
var objMaxName = objName.substring(0, (objName.length-3)) + "max";
if(document.getElementById(objMaxName) != undefined && document.getElementById(objMaxName) != "undefined" )
{
if(checkIntervalObjs(object, document.getElementById(objMaxName)) != true)
return false;
}
}
}
}
return true;
}
function checkForm(myform)
{
var i;
for (i=0;i<myform.elements.length;i++)
{
if (myform.elements[i].eos_displayname + "" == "undefined") continue;
if (myform.elements[i].eos_isnull=="false" && isnull(myform.elements[i].value)){
f_alert(myform.elements[i]," Can't be empty ");
return false;
}
if (checkInput(myform.elements[i])==false)
return false;
}
return true;
}
function checkIntervalObjs(obj1 , obj2)
{
var caption1 = obj1.getAttribute("eos_displayname");
var caption2 = obj2.getAttribute("eos_displayname");
var val1 = parseFloat(obj1.value);
var val2 = parseFloat(obj2.value);
//Elements with non-custom attributes are ignored & cake;
if (obj1.eos_displayname + "" == "undefined" || obj2.eos_displayname + "" == "undefined") {
return false;
}
//Comparison of date types & NBSP;
if(f_check_date(obj1) == true && f_check_date(obj2) == true){
var dtype = obj1.eos_datatype;
var format = dtype.substring(dtype.indexOf("(")+1,dtype.indexOf(")")); //Date format & NBSP;
val1 = getDateByFormat(obj1.value, format);
dtype = obj2.eos_datatype;
format = dtype.substring(dtype.indexOf("(")+1,dtype.indexOf(")")); //Date format & NBSP;
val2 = getDateByFormat(obj2.value, format);
if(val1 > val2){
obj2.select();
if(isVisible(obj) && checkPrVis(obj))
obj2.focus();
alert(caption1 + " The start date cannot be greater than its end date! ");
return false;
}
}
//Comparison of number types & NBSP;
if((isNaN(val1) && !isnull(val1)) || (isNaN(val2) && !isnull(val2))){
alert(caption1 + " Not all of the values of the Numbers can not be compared! ");
return false;
}
if(val1 > val2){
obj2.select();
if(isVisible(obj) && checkPrVis(obj))
obj2.focus();
alert(caption1 + " The starting value of can not be greater than its ending value! ");
return false;
}
return true;
}
function getDateByFormat(str){
var dateReg,format;
var y,M,d,H,m,s,yi,Mi,di,Hi,mi,si;
if((arguments[1] + "") == "undefined") format = "yyyy-MM-dd HH:mm:ss";
else format = arguments[1];
yi = format.indexOf("yyyy");
Mi = format.indexOf("MM");
di = format.indexOf("dd");
Hi = format.indexOf("HH");
mi = format.indexOf("mm");
si = format.indexOf("ss");
if(yi == -1 || Mi == -1 || di == -1) return null;
else{
y = parseInt(str.substring(yi, yi+4));
M = parseInt(str.substring(Mi, Mi+2));
d = parseInt(str.substring(di, di+2));
}
if(isNaN(y) || isNaN(M) || isNaN(d)) return null;
if(Hi == -1 || mi == -1 || si == -1) return new Date(y, M-1, d);
else{
H = str.substring(Hi, Hi+4);
m = str.substring(mi, mi+2);
s = str.substring(si, si+2);
}
if(isNaN(parseInt(y)) || isNaN(parseInt(M)) || isNaN(parseInt(d))) return new Date(y, M-1, d);
else return new Date(y, M-1, d,H, m, s);
}
function LTrim(str){
var whitespace = new String(" tnr"