Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
User can enter only 1,2,3.. to 31 number with , separated and user may write in short 1-31 or 1,2,3,4,5,6,7,8,9,10-30 in the textbox

plese help

What I have tried:

i am still trying but still not find any solution yet
Posted
Updated 23-May-16 8:06am
Comments
Prateek Dalbehera 23-May-16 13:59pm    
Just find a workaround, if it matches your requirement, you can try it:
Replace '-' in the string if any & then spilt wrt ','.

$("#txt").val().replace('-',',').spilt(',');
Now, in the array, there should be only integers... but you can also think of a REgex for the same.

1 solution

Basically, don't use a regex. it's possible, but it's horribly complex - partly because you don't want the user to be able to write "7-3", or "2,2,2,2,2", but you do want to allow "1, 3, 4, 17-21, 29" Regexes aren't good at comparing values to ensure they are in sequence!
Instead, use code (possibly with a regex to break out the numbers from the separators, which is pretty trivial) and look at what is generated there. It'll be a lot simpler, much easier to understand, and a lot, lot more maintainable in future.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900