详解Javascript判断Crontab表达式是否合法
Javascript判断Crontab表达式是否合法
这段时间在做Quartz任务调度,使用的Crontab表达式实现的。Crontab由前端页面输入,作为参数穿入后台。
虽然Quartz具有校验Crontab表达式的方法,如下:
booleancronExpressionFlag=CronExpression.isValidExpression(crontab);
但是我一直想在前端直接验证,即不需要通过异步的方式向后台获取验证结果,找了好久,发现没有现成的框架可以使用,于是自己根据网上搜索到的资料,写了这个js脚本。
这个脚本目前对日和星期的判断还有点小问题,不过不影响使用。
以后如果有时间,继续完善这个脚本,废话不多说了,上代码:
functioncronValidate(){ varcron=$("#cron").val(); varresult=CronExpressionValidator.validateCronExpression(cron); if(result==true){ alert("格式正确"); } else{ alert("格式错误"); } returnCronExpressionValidator.validateCronExpression(cron); } functionCronExpressionValidator(){ } CronExpressionValidator.validateCronExpression=function(value){ varresults=true; if(value==null||value.length==0){ returnfalse; } //splitandtestlength varexpressionArray=value.split(""); varlen=expressionArray.length; if((len!=6)&&(len!=7)){ returnfalse; } //checkonlyonequestionmark varmatch=value.match(/\?/g); if(match!=null&&match.length>1){ returnfalse; } //checkonlyonequestionmark vardayOfTheMonthWildcard=""; //ifappropriatelengthtestparts //[0]Seconds0-59,-*/ if(CronExpressionValidator.isNotWildCard(expressionArray[0],/[\*]/gi)){ if(!CronExpressionValidator.segmentValidator("([0-9\\\\,-\\/])",expressionArray[0],[0,59],"seconds")){ returnfalse; } } //[1]Minutes0-59,-*/ if(CronExpressionValidator.isNotWildCard(expressionArray[1],/[\*]/gi)){ if(!CronExpressionValidator.segmentValidator("([0-9\\\\,-\\/])",expressionArray[1],[0,59],"minutes")){ returnfalse; } } //[2]Hours0-23,-*/ if(CronExpressionValidator.isNotWildCard(expressionArray[2],/[\*]/gi)){ if(!CronExpressionValidator.segmentValidator("([0-9\\\\,-\\/])",expressionArray[2],[0,23],"hours")){ returnfalse; } } //[3]Dayofmonth1-31,-*?/LWC if(CronExpressionValidator.isNotWildCard(expressionArray[3],/[\*\?]/gi)){ if(!CronExpressionValidator.segmentValidator("([0-9LWC\\\\,-\\/])",expressionArray[3],[1,31],"daysofthemonth")){ returnfalse; } }else{ dayOfTheMonthWildcard=expressionArray[3]; } //[4]Month1-12orJAN-DEC,-*/ if(CronExpressionValidator.isNotWildCard(expressionArray[4],/[\*]/gi)){ expressionArray[4]=CronExpressionValidator.convertMonthsToInteger(expressionArray[4]); if(!CronExpressionValidator.segmentValidator("([0-9\\\\,-\\/])",expressionArray[4],[1,12],"months")){ returnfalse; } } //[5]Dayofweek1-7orSUN-SAT,-*?/LC# if(CronExpressionValidator.isNotWildCard(expressionArray[5],/[\*\?]/gi)){ expressionArray[5]=CronExpressionValidator.convertDaysToInteger(expressionArray[5]); if(!CronExpressionValidator.segmentValidator("([0-9LC#\\\\,-\\/])",expressionArray[5],[1,7],"daysoftheweek")){ returnfalse; } }else{ if(dayOfTheMonthWildcard==String(expressionArray[5])){ returnfalse; } } //[6]Yearemptyor1970-2099,-*/ if(len==7){ if(CronExpressionValidator.isNotWildCard(expressionArray[6],/[\*]/gi)){ if(!CronExpressionValidator.segmentValidator("([0-9\\\\,-\\/])",expressionArray[6],[1970,2099],"years")){ returnfalse; } } } returntrue; } //---------------------------------- //isNotWildcard静态方法; //---------------------------------- CronExpressionValidator.isNotWildCard=function(value,expression){ varmatch=value.match(expression); return(match==null||match.length==0)?true:false; } //---------------------------------- //convertDaysToInteger静态方法; //---------------------------------- CronExpressionValidator.convertDaysToInteger=function(value){ varv=value; v=v.replace(/SUN/gi,"1"); v=v.replace(/MON/gi,"2"); v=v.replace(/TUE/gi,"3"); v=v.replace(/WED/gi,"4"); v=v.replace(/THU/gi,"5"); v=v.replace(/FRI/gi,"6"); v=v.replace(/SAT/gi,"7"); returnv; } //---------------------------------- //convertMonthsToInteger静态方法; //---------------------------------- CronExpressionValidator.convertMonthsToInteger=function(value){ varv=value; v=v.replace(/JAN/gi,"1"); v=v.replace(/FEB/gi,"2"); v=v.replace(/MAR/gi,"3"); v=v.replace(/APR/gi,"4"); v=v.replace(/MAY/gi,"5"); v=v.replace(/JUN/gi,"6"); v=v.replace(/JUL/gi,"7"); v=v.replace(/AUG/gi,"8"); v=v.replace(/SEP/gi,"9"); v=v.replace(/OCT/gi,"10"); v=v.replace(/NOV/gi,"11"); v=v.replace(/DEC/gi,"12"); returnv; } //---------------------------------- //segmentValidator静态方法; //---------------------------------- CronExpressionValidator.segmentValidator=function(expression,value,range,segmentName){ varv=value; varnumbers=newArray(); //first,checkforanyimpropersegments varreg=newRegExp(expression,"gi"); if(!reg.test(v)){ returnfalse; } //checkduplicatetypes //checkonlyoneL vardupMatch=value.match(/L/gi); if(dupMatch!=null&&dupMatch.length>1){ returnfalse; } //lookthroughthesegments //breakupsegmentson',' //checkforspecialcasesL,W,C,/,#,- varsplit=v.split(","); vari=-1; varl=split.length; varmatch; while(++i-1 ||checkSegment.indexOf("l")>-1 ||checkSegment.indexOf("C")>-1 ||checkSegment.indexOf("c")>-1 ||checkSegment.indexOf("W")>-1||checkSegment .indexOf("w")>-1)){ //checkjustlorL if(checkSegment=="L"||checkSegment=="l") continue; pattern=/(\w*)\d+(l|c|w)?(\w*)/i; match=pattern.exec(checkSegment); //ifsomethingbeforeorafter if(!match||match[0]!=checkSegment){ continue; } //getthenumber varnumCheck=match[0]; numCheck=numCheck.replace(/(l|c|w)/ig,""); n=Number(numCheck); if(n&&!(isNaN(n))) numbers.push(n); elseif(match[0]=="0") numbers.push(n); continue; } varnumberSplit; //includes/ if(checkSegment.indexOf("/")>-1){ //takefirst# numberSplit=checkSegment.split("/"); if(numberSplit.length!=2){ continue; }else{ n=numberSplit[0]; if(n&&!(isNaN(n))) numbers.push(n); elseif(numberSplit[0]=="0") numbers.push(n); continue; } } //includes# if(checkSegment.indexOf("#")>-1){ //takefirst# numberSplit=checkSegment.split("#"); if(numberSplit.length!=2){ continue; }else{ n=numberSplit[0]; if(n&&!(isNaN(n))) numbers.push(n); elseif(numberSplit[0]=="0") numbers.push(n); continue; } } //includes- if(checkSegment.indexOf("-")>0){ //takeboth# numberSplit=checkSegment.split("-"); if(numberSplit.length!=2){ continue; }elseif(Number(numberSplit[0])>Number(numberSplit[1])){ continue; }else{ n=numberSplit[0]; if(n&&!(isNaN(n))) numbers.push(n); elseif(numberSplit[0]=="0") numbers.push(n); n=numberSplit[1]; if(n&&!(isNaN(n))) numbers.push(n); elseif(numberSplit[1]=="0") numbers.push(n); continue; } } } //lastly,checkthatallthefoundnumbersareinrange i=-1; l=numbers.length; if(l==0) returnfalse; while(++i range[1]){ returnfalse; } } returntrue; }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!