Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am trying to validate a textbox to accept user input in certain format with regular expressions. But unfortunately I was not able to get desired results.

I need user input as first 3 digits as fixed number 405 followed by year ranging 1950-current year and followed by six digit unique identifier, can I please get some help with this?

Any suggestions and help is appreciated.

Thanks.
Posted
Comments
Sergey Alexandrovich Kryukov 29-Oct-15 15:23pm    
You really want to abuse your users, this is bad.
—SA

Don't use a regex, or don;t use a regex alone.
They aren't good at "numeric ranges" - they are a text processing system.
So use a regex to break up the input:
^(?<prefix>\d{3})(?<year>\d{4})(?<uid>\d{6})$</uid></year></prefix>
And then use the three named groups to verify the actual content. It's more flexible, and a lot easier to understand (and make work next year as well...)
 
Share this answer
 
Comments
Member 12076824 29-Oct-15 18:42pm    
Thanks for the suggestion I will try this.
Member 12076824 5-Nov-15 18:38pm    
Can you please give more example or syntax for this?

I will really appreciate it.

Thanks.
Your UI, if you implement it as you want, will really be user-hostile.

Make it simpler for the user. If "405" is really always there (I doubt that though), just write it in some label, don't force the user to enter it. As to the year in the range, use the control System.Windows.Forms.NumericUpDown and set the range of values you want:
https://msdn.microsoft.com/en-us/library/system.windows.forms.numericupdown%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.numericupdown.minimum(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.numericupdown.maximum(v=vs.110).aspx[^].

If you use WPF, create a similar control of find out a similar one. See, for example: Creating a NumericUpDown control from scratch[^].

Another alternative is some kind of more specialized date/time picker.

—SA
 
Share this answer
 
v2
Comments
Member 12076824 29-Oct-15 18:43pm    
I was using regex for some of the simple text fields for alphabets and numbers only, so just tried to utilize same for this part. Thanks for the suggestion I will try this out.
Sergey Alexandrovich Kryukov 29-Oct-15 22:31pm    
You are welcome. Consider accepting this answer formally. Not allowing the user to make a mistake and no validation is always better than validation.
—SA
 
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