91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

詳解Javascript判斷Crontab表達式是否合法

發布時間:2020-09-26 02:14:11 來源:腳本之家 閱讀:211 作者:lqh 欄目:編程語言

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; 
  } 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

苍梧县| 清涧县| 敦煌市| 清流县| 雷山县| 中江县| 云梦县| 井冈山市| 拜泉县| 芮城县| 呈贡县| 廉江市| 天祝| 云龙县| 新余市| 凤翔县| 嘉禾县| 宁夏| 夹江县| 搜索| 泰顺县| 洪湖市| 宝鸡市| 新宾| 土默特左旗| 苍山县| 安义县| 石首市| 宜阳县| 沈丘县| 芜湖市| 呼和浩特市| 临江市| 宝坻区| 芦山县| 铅山县| 沧源| 蓝田县| 肥东县| 丹凤县| 遂平县|