The problem is next. I created an input field which has validation and this is valid data:
- 1-12, 14, 16, 22, 25-35, 41, 49, 55-90
- 1230-1992, 2001-2099, 9931
- 1-2
- 13
- 1,3,4,5,6,10
- all
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.