详解Javascript判断Crontab表达式是否合法
程序员文章站
2024-03-03 18:42:58
javascript判断crontab表达式是否合法
这段时间在做quartz任务调度,使用的crontab表达式实现的。crontab由前端页面输入,作为参数穿入后台。...
javascript判断crontab表达式是否合法
这段时间在做quartz任务调度,使用的crontab表达式实现的。crontab由前端页面输入,作为参数穿入后台。
虽然quartz具有校验crontab表达式的方法,如下:
boolean cronexpressionflag = cronexpression.isvalidexpression(crontab);
但是我一直想在前端直接验证,即不需要通过异步的方式向后台获取验证结果,找了好久,发现没有现成的框架可以使用,于是自己根据网上搜索到的资料,写了这个js脚本。
这个脚本目前对日和星期的判断还有点小问题,不过不影响使用。
以后如果有时间,继续完善这个脚本,废话不多说了,上代码:
function cronvalidate() { var cron = $("#cron").val(); var result = cronexpressionvalidator.validatecronexpression(cron); if(result == true){ alert("格式正确"); } else{ alert("格式错误"); } return cronexpressionvalidator.validatecronexpression(cron); } function cronexpressionvalidator() { } cronexpressionvalidator.validatecronexpression = function(value) { var results = true; if (value == null || value.length == 0) { return false; } // split and test length var expressionarray = value.split(" "); var len = expressionarray.length; if ((len != 6) && (len != 7)) { return false; } // check only one question mark var match = value.match(/\?/g); if (match != null && match.length > 1) { return false; } // check only one question mark var dayofthemonthwildcard = ""; // if appropriate length test parts // [0] seconds 0-59 , - * / if (cronexpressionvalidator.isnotwildcard(expressionarray[0], /[\*]/gi)) { if (!cronexpressionvalidator.segmentvalidator("([0-9\\\\,-\\/])", expressionarray[0], [0, 59], "seconds")) { return false; } } // [1] minutes 0-59 , - * / if (cronexpressionvalidator.isnotwildcard(expressionarray[1], /[\*]/gi)) { if (!cronexpressionvalidator.segmentvalidator("([0-9\\\\,-\\/])", expressionarray[1], [0, 59], "minutes")) { return false; } } // [2] hours 0-23 , - * / if (cronexpressionvalidator.isnotwildcard(expressionarray[2], /[\*]/gi)) { if (!cronexpressionvalidator.segmentvalidator("([0-9\\\\,-\\/])", expressionarray[2], [0, 23], "hours")) { return false; } } // [3] day of month 1-31 , - * ? / l w c if (cronexpressionvalidator.isnotwildcard(expressionarray[3], /[\*\?]/gi)) { if (!cronexpressionvalidator.segmentvalidator("([0-9lwc\\\\,-\\/])", expressionarray[3], [1, 31], "days of the month")) { return false; } } else { dayofthemonthwildcard = expressionarray[3]; } // [4] month 1-12 or jan-dec , - * / if (cronexpressionvalidator.isnotwildcard(expressionarray[4], /[\*]/gi)) { expressionarray[4] = cronexpressionvalidator.convertmonthstointeger(expressionarray[4]); if (!cronexpressionvalidator.segmentvalidator("([0-9\\\\,-\\/])", expressionarray[4], [1, 12], "months")) { return false; } } // [5] day of week 1-7 or sun-sat , - * ? / l c # if (cronexpressionvalidator.isnotwildcard(expressionarray[5], /[\*\?]/gi)) { expressionarray[5] = cronexpressionvalidator.convertdaystointeger(expressionarray[5]); if (!cronexpressionvalidator.segmentvalidator("([0-9lc#\\\\,-\\/])", expressionarray[5], [1, 7], "days of the week")) { return false; } } else { if (dayofthemonthwildcard == string(expressionarray[5])) { return false; } } // [6] year empty or 1970-2099 , - * / if (len == 7) { if (cronexpressionvalidator.isnotwildcard(expressionarray[6], /[\*]/gi)) { if (!cronexpressionvalidator.segmentvalidator("([0-9\\\\,-\\/])", expressionarray[6], [1970, 2099], "years")) { return false; } } } return true; } // ---------------------------------- // isnotwildcard 静态方法; // ---------------------------------- cronexpressionvalidator.isnotwildcard = function(value, expression) { var match = value.match(expression); return (match == null || match.length == 0) ? true : false; } // ---------------------------------- // convertdaystointeger 静态方法; // ---------------------------------- cronexpressionvalidator.convertdaystointeger = function(value) { var v = 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"); return v; } // ---------------------------------- // convertmonthstointeger 静态方法; // ---------------------------------- cronexpressionvalidator.convertmonthstointeger = function(value) { var v = 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"); return v; } // ---------------------------------- // segmentvalidator 静态方法; // ---------------------------------- cronexpressionvalidator.segmentvalidator = function(expression, value, range, segmentname) { var v = value; var numbers = new array(); // first, check for any improper segments var reg = new regexp(expression, "gi"); if (!reg.test(v)) { return false; } // check duplicate types // check only one l var dupmatch = value.match(/l/gi); if (dupmatch != null && dupmatch.length > 1) { return false; } // look through the segments // break up segments on ',' // check for special cases l,w,c,/,#,- var split = v.split(","); var i = -1; var l = split.length; var match; while (++i < l) { // set vars var checksegment = split[i]; var n; var pattern = /(\w*)/; match = pattern.exec(checksegment); // if just number pattern = /(\w*)\-?\d+(\w*)/; match = pattern.exec(checksegment); if (match && match[0] == checksegment && checksegment.indexof("l") == -1 && checksegment.indexof("l") == -1 && checksegment.indexof("c") == -1 && checksegment.indexof("c") == -1 && checksegment.indexof("w") == -1 && checksegment.indexof("w") == -1 && checksegment.indexof("/") == -1 && (checksegment.indexof("-") == -1 || checksegment .indexof("-") == 0) && checksegment.indexof("#") == -1) { n = match[0]; if (n && !(isnan(n))) numbers.push(n); else if (match[0] == "0") numbers.push(n); continue; } // includes l, c, or w pattern = /(\w*)l|c|w(\w*)/i; match = pattern.exec(checksegment); if (match && match[0] != "" && (checksegment.indexof("l") > -1 || checksegment.indexof("l") > -1 || checksegment.indexof("c") > -1 || checksegment.indexof("c") > -1 || checksegment.indexof("w") > -1 || checksegment .indexof("w") > -1)) { // check just l or l if (checksegment == "l" || checksegment == "l") continue; pattern = /(\w*)\d+(l|c|w)?(\w*)/i; match = pattern.exec(checksegment); // if something before or after if (!match || match[0] != checksegment) { continue; } // get the number var numcheck = match[0]; numcheck = numcheck.replace(/(l|c|w)/ig, ""); n = number(numcheck); if (n && !(isnan(n))) numbers.push(n); else if (match[0] == "0") numbers.push(n); continue; } var numbersplit; // includes / if (checksegment.indexof("/") > -1) { // take first # numbersplit = checksegment.split("/"); if (numbersplit.length != 2) { continue; } else { n = numbersplit[0]; if (n && !(isnan(n))) numbers.push(n); else if (numbersplit[0] == "0") numbers.push(n); continue; } } // includes # if (checksegment.indexof("#") > -1) { // take first # numbersplit = checksegment.split("#"); if (numbersplit.length != 2) { continue; } else { n = numbersplit[0]; if (n && !(isnan(n))) numbers.push(n); else if (numbersplit[0] == "0") numbers.push(n); continue; } } // includes - if (checksegment.indexof("-") > 0) { // take both # numbersplit = checksegment.split("-"); if (numbersplit.length != 2) { continue; } else if (number(numbersplit[0]) > number(numbersplit[1])) { continue; } else { n = numbersplit[0]; if (n && !(isnan(n))) numbers.push(n); else if (numbersplit[0] == "0") numbers.push(n); n = numbersplit[1]; if (n && !(isnan(n))) numbers.push(n); else if (numbersplit[1] == "0") numbers.push(n); continue; } } } // lastly, check that all the found numbers are in range i = -1; l = numbers.length; if (l == 0) return false; while (++i < l) { // alert(numbers[i]); if (numbers[i] < range[0] || numbers[i] > range[1]) { return false; } } return true; }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!