简体   繁体   中英

Regular Expression for Cron Expression in Javascript

I'm trying to figure out a correct validation for custom cron inputs. Here's what I have.

Html

<input id="freqInput" type="text" placeholder="Enter CRONJOB" class="form-control" />

Javascript

function isCronValid(freq) {
    var cronregex = new RegExp("/^(\*|((\*\/)?[1-5]?[0-9])) (\*|((\*\/)?[1-5]?[0-9])) (\*|((\*\/)?(1?[0-9]|2[0-3]))) (\*|((\*\/)?([1-9]|[12][0-9]|3[0-1]))) (\*|((\*\/)?([1-9]|1[0-2]))) (\*|((\*\/)?[0-6]))$/");
    return cronregex.test(freq);
}

Am I missing something? It's not working.

Controller

public string HangfireCronJob(string url, string freqInput)
{
    return freqInput.ToString();  
}

It appears your regex expression is invalid. Try this:

 document.getElementById('validate').addEventListener('click', () =>{ console.log(isCronValid(document.getElementById('freqInput').value)); }); function isCronValid(freq) { var cronregex = new RegExp(/^(\\*|([0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])|\\*\\/([0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])) (\\*|([0-9]|1[0-9]|2[0-3])|\\*\\/([0-9]|1[0-9]|2[0-3])) (\\*|([1-9]|1[0-9]|2[0-9]|3[0-1])|\\*\\/([1-9]|1[0-9]|2[0-9]|3[0-1])) (\\*|([1-9]|1[0-2])|\\*\\/([1-9]|1[0-2])) (\\*|([0-6])|\\*\\/([0-6]))$/); return cronregex.test(freq); } 
 <input id="freqInput" type="text" placeholder="Enter CRONJOB" class="form-control" value="* * * * *" /> <button id="validate">Validate</button> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM