Yesterday I've got a task to implement a validation on the field where user can enter the range of pages that he wants to download.
After reading some tutorials, I've created such pattern which in my opinion should work, but it doesn't :(
Can you give me a hint where is the mistake, or how it should be done in the better way.
<script type="text/javascript">
var patt1=new RegExp("^(\s*\d+\s*\-\s*\d+\s*,?|\s*\d+\s*,?)+$");
document.write(patt1.test("1, 2, 3-5, 6, 8, 10-12"));
</script>
P.S. You can test it here: http://www.w3schools.com/js/tryit.asp?filename=tryjs_regexp_test
More examples:
- 1 match
- 1-2 match
- -2 not match
- 1, 2-3, 4, 5-7 match
- 1 2, 3 not match
- 1-2-2 not match
etc... like in MS Office or Adobe PDF Reader