Reputation: 43
Im trying to create a Regex mask in order to restrict an input field.
So far I came up with this
^[^0][0-9]*(,?[0-9]*)*$
The following should be the accepted values:
123456,12,1,654,19512
2321
312,1
Basically any given integer followed by a comma and another integer (the number of digits doesnt matter) but any group of integer should not start with a zero.
But I missing some extra expression since the following values are accepted and it shouldnt:
123,012
123,,0123
123,0,0
123,0,0,,,,0,31
I will truly appreciate your help, since Im new with Regex codes.
Edit: I know that the value might have a trailing comma but thats ok, I already have an input validation after the value is submitted
Also how can I allow the backspace to work within the input?
Upvotes: 2
Views: 1914
Reputation:
This regex might work if you are validating between keystrokes.
A different regex is needed after acceptance.
// ^(?![0,]|[\d,]*,0$|[\d,]*,,$)[\d,]+$
^ # BOL
(?! # Not any of these
[0,] # No 0 or , at beginning of string
| [\d,]* ,0 $ # No ,0 at end of string
| [\d,]* ,, $ # No ,, at end of string
)
[\d,]+ # validate these to restrict input chars
$ # EOL
Upvotes: 0
Reputation: 899
I wanted to add a comment, but ended up writing too much so I'm transfering this to an answer.
The regex Guillaume Poussel and Luis Gonzalez wrote are both what you want (although they differ slightly in syntax and more importantly, Luis' regex allows whitespace between the commas and integers) - for end validation. As Guillame Poussel pointed out in his comment, you need to do your validation in two steps - one as an input mask and one as an all-done validation. The regex they provided you with is for the all done part - you might want to put this regex on the blur event of the input control. For the keypress or similar event that controls input (cancels users' keystrokes) you need a more loose regex that allows trailing commas, leading zeros and even strings ending in multiple commas, i.e.
^[1-9][0-9]*(,[0-9]*)*$
.
If you are unhappy with the client being able to insert an invalid input string and being told nothing's wrong with it, but then being notified that it's invalid after going to another field or something, you could also have that one regex you already got as an answer, but instead of having it as a mask regex (cancelling the keypress if the input is not valid), you could have that regex change the background of the field to red or something like that to notify the person right away that the input, as it currently is is not valid.
You could also do a mixed solution where you have a mask input that's loose like the one I suggested and the final regex on the same event, but instead of cancelling the keypress on failing the all-done regex, change the background to red or whatever and cancel only if the loose regex fails. This way your user is restrained from writing most invalid input strings and for those input strings that are not valid, (s)he is notified right away that it is not valid.
Upvotes: 0
Reputation: 101
This'll do.
^[1-9]\d*(,\s*[1-9]\d*)*$
Anythign that starts with 1-9 followed by 0 or more digits, followd by a the same construct with the prepended comma 0 or more times
Upvotes: 1
Reputation: 9822
You should not mark the comma as optional:
^[1-9][0-9]*(,[1-9][0-9]*)*$
Upvotes: 3