how to restrict textbox with specific format using asp.net regular expression validator

I have text which is binded to asp.net regular expression validation. I want max length of character as 13 and want to have format like xx/xxxxx-xxxx

This are valid formats

33/34567-1
33/12345-12
33/12345-123
33/12345-1234

I tried this

<asp:TextBox ID="txt1" runat="server" OnTextChanged="txt1_TextChanged" class="textNormal" MaxLength="13" Width="100"></asp:TextBox> (xx/xxxxx-xxxx)
                    <br />
                    <asp:RegularExpressionValidator ID="regexValidator1" runat="server"
                     ErrorMessage="Must be in format xx/xxxxx-xxxx" ValidationExpression="dd/ddddd-d{4}$"
                    ControlToValidate="txt1">Must be in format xx/xxxxx-xxxx</asp:RegularExpressionValidator>

But its not working as i want. When i type 33/45678-12 then it throws validation error.
only one character after Last part after -is mandatory.

How to fix this?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can shorten the 5 digits to d{5} and match 1-4 digits at the end using d{1,4} which will still have a MaxLength="13"

ValidationExpression="dd/d{5}-d{1,4}$"


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x