The problem is next. I created an input field which has validation and this is valid data:
Basically, any combination of those numbers (ranges, comma-separated ranges, comma-separated number, spaces after commas, no spaces after commas, word: 'all')
My RegEx: /^(([0-9]{0,4},?\s{0,})+([0-9]{1,4}-[0-9]{1,4}){0,},?\s{0,})+$|^(all)$|^([0-9]{1,4}-[0-9]{1,4}){0,},?\s{0,}$/
It works almost fine, there is only 1 major problem.
When I start typing, and after some comma-separated numbers I add something that is not valid, like characters: 'abc' - and at this moment my browser tab stucks.
Try this as an input: 1-12, 14, 16, 19, 20-29, was
Fiddle to test: http://jsfiddle.net/c9ahfhqy/
Is there any suggestions on how the proper RegEx should look like?
Code:
$("input[type='text']").blur(function() { //when you lose focus on input
doneTyping();
});
function doneTyping() {
if(!$('#inputToCheck').val().match(/^(([0-9]{0,4},?\s{0,})+([0-9]{1,4}\-[0-9]{1,4}){0,},?\s{0,})+$|^(all)$|^([0-9]{1,4}\-[0-9]{1,4}){0,},?\s{0,}$/)) {
console.log("Not Valid");
} else {
console.log("Valid");
}
}
Really appreciate help.
As @derp said, your regex has a problem of Catastrophic Backtracking.
The following one seems to work like you want, and doesn't freeze the browser:
/^(?:all|\d{1,4}(?:-\d{1,4})?(?:,\s*\d{1,4}(?:-\d{1,4})?)*)$/
Demo
Using RegexBuddy's debugger to test 1-12, 14, 16, 19, 20-29, was
,
You regex fails after attempting 1000000 steps, with this error:
Your regular expression is too complex to continue debugging.
The regex engine you plan to use it with may not be able to handle it at all and crash.
Look up "catastrophic backtracking" in the help file to learn how to avoid this situation.
The regex above fails after 96 steps, with no error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With