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();
}
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();
}
Share
Improve this question
edited Jul 9, 2019 at 4:30
Arnaud
7,44910 gold badges52 silver badges72 bronze badges
asked Sep 5, 2018 at 16:25
Mathew DodsonMathew Dodson
1251 gold badge1 silver badge10 bronze badges
2
- What exactly are you trying to test? – Luca Kiebel Commented Sep 5, 2018 at 16:27
- Updated post to make it clearer – Mathew Dodson Commented Sep 5, 2018 at 16:33
1 Answer
Reset to default 4It 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>