真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

詳解Javascript判斷Crontab表達(dá)式是否合法

Javascript判斷Crontab表達(dá)式是否合法

創(chuàng)新互聯(lián)建站堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站建設(shè)、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的浦東網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

撫松網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、APP開(kāi)發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項(xiàng)目制作,到程序開(kāi)發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)2013年至今到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專(zhuān)注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:申請(qǐng)域名、虛擬主機(jī)、營(yíng)銷(xiāo)軟件、網(wǎng)站建設(shè)、應(yīng)城網(wǎng)站維護(hù)、網(wǎng)站推廣。

創(chuàng)新互聯(lián)是一家專(zhuān)業(yè)提供建平企業(yè)網(wǎng)站建設(shè),專(zhuān)注與成都做網(wǎng)站、成都網(wǎng)站制作、成都h5網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為建平眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專(zhuān)業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。

創(chuàng)新互聯(lián)建站是一家專(zhuān)注于網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)與策劃設(shè)計(jì),太湖網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專(zhuān)注于網(wǎng)站建設(shè)十載,網(wǎng)設(shè)計(jì)領(lǐng)域的專(zhuān)業(yè)建站公司;建站業(yè)務(wù)涵蓋:太湖等地區(qū)。太湖做網(wǎng)站價(jià)格咨詢:18980820575

創(chuàng)新互聯(lián)是一家專(zhuān)注于做網(wǎng)站、網(wǎng)站制作與策劃設(shè)計(jì),高平網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專(zhuān)注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專(zhuān)業(yè)建站公司;建站業(yè)務(wù)涵蓋:高平等地區(qū)。高平做網(wǎng)站價(jià)格咨詢:18982081108

這段時(shí)間在做Quartz任務(wù)調(diào)度,使用的Crontab表達(dá)式實(shí)現(xiàn)的。Crontab由前端頁(yè)面輸入,作為參數(shù)穿入后臺(tái)。
雖然Quartz具有校驗(yàn)Crontab表達(dá)式的方法,如下:

boolean cronExpressionFlag = CronExpression.isValidExpression(crontab);

但是我一直想在前端直接驗(yàn)證,即不需要通過(guò)異步的方式向后臺(tái)獲取驗(yàn)證結(jié)果,找了好久,發(fā)現(xiàn)沒(méi)有現(xiàn)成的框架可以使用,于是自己根據(jù)網(wǎng)上搜索到的資料,寫(xiě)了這個(gè)js腳本。

這個(gè)腳本目前對(duì)日和星期的判斷還有點(diǎn)小問(wèn)題,不過(guò)不影響使用。

以后如果有時(shí)間,繼續(xù)完善這個(gè)腳本,廢話不多說(shuō)了,上代碼:

 function cronValidate() {
    var cron = $("#cron").val();
    var result = CronExpressionValidator.validateCronExpression(cron);
    if(result == true){
      alert("格式正確"); 
    }
    else{
      alert("格式錯(cuò)誤");
    }
    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 靜態(tài)方法; 
  // ---------------------------------- 
  CronExpressionValidator.isNotWildCard = function(value, expression) { 
    var match = value.match(expression); 
    return (match == null || match.length == 0) ? true : false; 
  } 

  // ---------------------------------- 
  // convertDaysToInteger 靜態(tài)方法; 
  // ---------------------------------- 
  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 靜態(tài)方法; 
  // ---------------------------------- 
  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 靜態(tài)方法; 
  // ---------------------------------- 
  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; 
  } 

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


網(wǎng)頁(yè)題目:詳解Javascript判斷Crontab表達(dá)式是否合法
URL鏈接:http://weahome.cn/article/igchcj.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部