i tried to create a Regex to allow only in input in a range from below format with single hyphen.
Match
1-45
2-10
12-45
Not Match
1-
-2
1-45-
1,45
145
but most of the regex allow multiple hyphen
>Solution :
Assuming you are using JavaScript, I prefer a splitting approach here:
var input = "12-45";
var pass = false;
if (/^\d+-\d+$/.test(input)) {
var nums = input.split("-");
if (nums[0] >= 1 && nums[0] <= 45 && nums[1] >= 1 && nums[1] <= 45) {
pass = true;
}
}
if (pass) {
console.log("VALID");
}
else {
console.log("INVALID");
}