JavaScript正则表达式校验与递归函数实际应用实例解析
JS递归函数(菲波那切数列)
实例解析: 
一组数字:011235813
01234567
sl(0)=0;
sl(1)=1;
sl(2)=sl(0)+sl(1);
sl(3)=sl(1)+sl(2);
functionsl(i){
if(i==0){
return0;
}elseif(i==1){
return1;
}else{
returnsl(i-1)+sl(i-2);
}
}
正则表达式检验
//校验是否全由数字组成
functionisDigit(s)
{
varpatrn=/^[0-9]{1,20}$/;
if(!patrn.exec(s))returnfalse
returntrue
}
//校验登录名:只能输入5-20个以字母开头、可带数字、“_”、“.”的字串
functionisRegisterUserName(s)
{
varpatrn=/^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$/;
if(!patrn.exec(s))returnfalse
returntrue
}
//校验用户姓名:只能输入1-30个以字母开头的字串
functionisTrueName(s)
{
varpatrn=/^[a-zA-Z]{1,30}$/;
if(!patrn.exec(s))returnfalse
returntrue
}
//校验密码:只能输入6-20个字母、数字、下划线
functionisPasswd(s)
{
varpatrn=/^(\w){6,20}$/;
if(!patrn.exec(s))returnfalse
returntrue
}
//校验普通电话、传真号码:可以“+”开头,除数字外,可含有“-”
functionisTel(s)
{
//varpatrn=/^[+]{0,1}(\d){1,3}[]?([-]?(\d){1,12})+$/;
varpatrn=/^[+]{0,1}(\d){1,3}[]?([-]?((\d)|[]){1,12})+$/;
if(!patrn.exec(s))returnfalse
returntrue
}
//校验手机号码:必须以数字开头,除数字外,可含有“-”
functionisMobil(s)
{
varpatrn=/^[+]{0,1}(\d){1,3}[]?([-]?((\d)|[]){1,12})+$/;
if(!patrn.exec(s))returnfalse
returntrue
}
//校验邮政编码
functionisPostalCode(s)
{
//varpatrn=/^[a-zA-Z0-9]{3,12}$/;
varpatrn=/^[a-zA-Z0-9]{3,12}$/;
if(!patrn.exec(s))returnfalse
returntrue
}
//校验搜索关键字
functionisSearch(s)
{
varpatrn=/^[^`~!@#$%^&*()+=|\\\][\]\{\}:;\'\,.<>/?]{1}[^`~!@$%^&()+=|\\\][\]\{\}:;\'\,.<>?]{0,19}$/;
if(!patrn.exec(s))returnfalse
returntrue
}
functionisIP(s)//byzergling
{
varpatrn=/^[0-9.]{1,20}$/;
if(!patrn.exec(s))returnfalse
returntrue
}
*FUNCTION:isBetween
*PARAMETERS:valASanyvalue
*loASLowerlimittocheck
*hiASHigherlimittocheck
*CALLS:NOTHING
*RETURNS:TRUEifvalisbetweenloandhibothinclusive,otherwisefalse.
functionisBetween(val,lo,hi){
if((valhi)){return(false);}
else{return(true);}
}
*FUNCTION:isDatechecksavaliddate
*PARAMETERS:theStrASString
*CALLS:isBetween,isInt
*RETURNS:TRUEiftheStrisavaliddateotherwisefalse.
functionisDate(theStr){
varthe1st=theStr.indexOf('-');
varthe2nd=theStr.lastIndexOf('-');
if(the1st==the2nd){return(false);}
else{
vary=theStr.substring(0,the1st);
varm=theStr.substring(the1st+1,the2nd);
vard=theStr.substring(the2nd+1,theStr.length);
varmaxDays=31;
if(isInt(m)==false||isInt(d)==false||isInt(y)==false){
return(false);}
elseif(y.length<4){return(false);}
elseif(!isBetween(m,1,12)){return(false);}
elseif(m==4||m==6||m==9||m==11)maxDays=30;
elseif(m==2){
if(y%4>0)maxDays=28;
elseif(y%100==0&&y%400>0)maxDays=28;
elsemaxDays=29;
}
if(isBetween(d,1,maxDays)==false){return(false);}
else{return(true);}
}
}
*FUNCTION:isEuDatechecksavaliddateinBritishformat
*PARAMETERS:theStrASString
*CALLS:isBetween,isInt
*RETURNS:TRUEiftheStrisavaliddateotherwisefalse.
functionisEuDate(theStr){
if(isBetween(theStr.length,8,10)==false){return(false);}
else{
varthe1st=theStr.indexOf('/');
varthe2nd=theStr.lastIndexOf('/');
if(the1st==the2nd){return(false);}
else{
varm=theStr.substring(the1st+1,the2nd);
vard=theStr.substring(0,the1st);
vary=theStr.substring(the2nd+1,theStr.length);
varmaxDays=31;
if(isInt(m)==false||isInt(d)==false||isInt(y)==false){
return(false);}
elseif(y.length<4){return(false);}
elseif(isBetween(m,1,12)==false){return(false);}
elseif(m==4||m==6||m==9||m==11)maxDays=30;
elseif(m==2){
if(y%4>0)maxDays=28;
elseif(y%100==0&&y%400>0)maxDays=28;
elsemaxDays=29;
}
if(isBetween(d,1,maxDays)==false){return(false);}
else{return(true);}
}
}
}
*FUNCTION:CompareDate!Whichisthelatest!
*PARAMETERS:lessDate,moreDateASString
*CALLS:isDate,isBetween
*RETURNS:TRUEiflessDateDate2){return(false);}
return(true);
}
*FUNCTIONisEmptychecksiftheparameterisemptyornull
*PARAMETERstrASString
functionisEmpty(str){
if((str==null)||(str.length==0))returntrue;
elsereturn(false);
}
*FUNCTION:isInt
*PARAMETER:theStrASString
*RETURNS:TRUEifthepassedparameterisaninteger,otherwiseFALSE
*CALLS:isDigit
functionisInt(theStr){
varflag=true;
if(isEmpty(theStr)){flag=false;}
else
{for(vari=0;idecLen)return(false);
elseif(!isInt(intPart)||!isInt(decPart))return(false);
elseif(isEmpty(decPart))return(false);
elsereturn(true);
}
}
*FUNCTION:isEmail
*PARAMETER:String(EmailAddress)
*RETURNS:TRUEiftheStringisavalidEmailaddress
*FALSEifthepassedstringisnotavalidEmailAddress
*EMAILFORMAT:AnyName@EmailServere.g;webmaster@hotmail.com
*@signcanappearonlyonceintheemailaddress.
functionisEmail(theStr){
varatIndex=theStr.indexOf('@');
vardotIndex=theStr.indexOf('.',atIndex);
varflag=true;
theSub=theStr.substring(0,dotIndex+1)
if((atIndex<1)||(atIndex!=theStr.lastIndexOf('@'))||(dotIndexDocumenttoopeninthenewwindow
hite->Heightofthenewwindow
wide->Widthofthenewwindow
bars->1-Scrollbars=YES0-ScrollBars=NO
resize->1-Resizable=YES0-Resizable=NO
*CALLS:NONE
*RETURNS:Newwindowinstance
functionnewWindow(doc,hite,wide,bars,resize){
varwinNew="_blank";
varopt="toolbar=0,location=0,directories=0,status=0,menubar=0,";
opt+=("scrollbars="+bars+",");
opt+=("resizable="+resize+",");
opt+=("width="+wide+",");
opt+=("height="+hite);
winHandle=window.open(doc,winNew,opt);
return;
}
*FUNCTION:DecimalFormat
*PARAMETERS:paramValue->Fieldvalue
*CALLS:NONE
*RETURNS:Formatedstring
functionDecimalFormat(paramValue){
varintPart=parseInt(paramValue);
vardecPart=parseFloat(paramValue)-intPart;
str="";
if((decPart==0)||(decPart==null))str+=(intPart+".00");
elsestr+=(intPart+decPart);
return(str);
}    
正则表达式应用
"^\\d+$"//非负整数(正整数+0) "^[0-9]*[1-9][0-9]*$"//正整数 "^((-\\d+)|(0+))$"//非正整数(负整数+0) "^-[0-9]*[1-9][0-9]*$"//负整数 "^-?\\d+$"//整数 "^\\d+(\\.\\d+)?$"//非负浮点数(正浮点数+0) "^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$"//正浮点数 "^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$"//非正浮点数(负浮点数+0) "^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"//负浮点数 "^(-?\\d+)(\\.\\d+)?$"//浮点数 "^[A-Za-z]+$"//由26个英文字母组成的字符串 "^[A-Z]+$"//由26个英文字母的大写组成的字符串 "^[a-z]+$"//由26个英文字母的小写组成的字符串 "^[A-Za-z0-9]+$"//由数字和26个英文字母组成的字符串 "^\\w+$"//由数字、26个英文字母或者下划线组成的字符串 "^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$"//email地址 "^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$"//url
递归函数应用
总结
以上所述是小编给大家介绍的JavaScript正则表达式校验与递归函数实际应用实例解析,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!