This article mainly introduces detailed information about how to use Javascript to determine whether the Crontab expression is legal. For more information about how to use Javascript to determine whether the Crontab expression is legal, for more information, see
Javascript checks whether the Crontab expression is legal
During this time, the Quartz task scheduling is implemented using the Crontab expression. Crontab is input on the front-end page and is used as a parameter to enter the background.
Although Quartz has the following method to verify the Crontab expression:
boolean cronExpressionFlag = CronExpression.isValidExpression(crontab);
However, I always wanted to directly verify on the front-end, that is, I didn't need to obtain the verification result from the backend in asynchronous mode. After searching for a long time, I found that there was no ready-made framework to use, so I wrote this js script based on the information I found on the Internet.
This script currently has minor issues with day and week judgment, but it does not affect usage.
In the future, if you have time to continue to improve this script, we will not talk much about it. Go to the Code:
Function cronValidate () {var cron = $ ("# cron "). val (); var result = CronExpressionValidator. validateCronExpression (cron); if (result = true) {alert ("correct format");} else {alert ("Incorrect format");} return CronExpressionValidator. validateCronExpression (cron);} function CronExpressionValidator () {} CronExpressionValidator. validateCronExpression = function (value) {var results = true; if (value = null | value. len Splits = 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,209 9], "years ")) {return false ;}}return true;} // -------------------------------------- // isNotWildcard static method; // ------------------------------------ CronExpressionValidator. isNotWildCard = function (value, expression) {var match = value. match (expression); return (match = null | match. length = 0 )? True: false;} // ---------------------------------- // convertDaysToInteger static method; // ------------------------------------ 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 static method; // ---------------------------------- 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 static method; // -------------------------------------- CronExpressionValidator. segmentValidator = function (expression, value, range, segmentName) {var v = value; var numbers = new Ar Ray (); // 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.exe c (checkSegment); // if just number pattern =/(\ w *)\-? \ D + (\ w *)/; match = pattern.exe c (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.exe c (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.exe c (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 ;}
The above is a detailed description of the sample code for Javascript to determine whether the Crontab expression is valid. For more information, see other related articles in the first PHP community!