Validation on multiple controls with regex

How do I validate multiple textbox's with regex.
Like Validate one textbox for curency & another for AlphaNumeric.
u2envy1Asked:
Who is Participating?
 
Anurag ThakurConnect With a Mentor Technical ManagerCommented:
the previous expression was able to take 22.22 but was also taking 22 without any decimal places which i think is not your requirement
i have modified the expression a little "^\d{0,4}\.\d{2}?$"
hth ragi
0
 
Anurag ThakurTechnical ManagerCommented:
its a windows application or a web
in case of web application you will have to add a regularexpressionvalidator for every control and give a customized regular expression

in case of a windows application you will have to validate manually before you save the data
0
 
u2envy1Author Commented:
Windows.....
I got this for currency but its not working.

 static public bool IsCurrency(String strToCheck)
        {
            Regex objCurrencyPattern = new Regex(@"^\$?[0-9]+(,[0-9]{3})*(\.[0-9]{2})?$");
            return !objCurrencyPattern.IsMatch(strToCheck);
            //^\$?[0-9]+(,[0-9]{3})*(\.[0-9]{2})?$
            //[^a-zA-Z0-9]
        }
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
Anurag ThakurTechnical ManagerCommented:
what is your requirement for the currency...
i am not too good with regular expresions but i will give it a try if you can give me your requirements for the currency?
0
 
u2envy1Author Commented:
It can contain any number 0-9 but must have two decimal places at the end.
9999.99
0
 
Anurag ThakurTechnical ManagerCommented:
"^\d{0,4}(\.\d{2})?$"
0
 
u2envy1Author Commented:
If i add 23.25 it does not take it.
This is the format #######.## It could be less but not more.
0
 
Anurag ThakurTechnical ManagerCommented:
my code snippet that works

<asp:ValidationSummary ID="vsPrice" runat="server" DisplayMode="BulletList" ValidationGroup="Price" />
<asp:TextBox ID="txtServicePrice" runat="server" MaxLength="10" Text="0.0"></asp:TextBox>
<asp:RegularExpressionValidator ID="revServicePrice" runat="server" ControlToValidate="txtServicePrice" Display="dynamic" ErrorMessage="error" SetFocusOnError="true" Text="*" ToolTip="error" ValidationExpression="^\d{0,4}\.\d{2}?$" ValidationGroup="Price"></asp:RegularExpressionValidator>
<asp:Button ID="btnPriceAdd" runat="server" OnClick="Button_Click" Text="Add" ValidationGroup="Price" />

Open in new window

0
 
u2envy1Author Commented:
Im using this on a windows form & not asp.net. I will try it again & let you know.
0
 
Anurag ThakurTechnical ManagerCommented:
the regular expression will work whether its a win form or a web form
0
 
u2envy1Author Commented:
I had to dive in & learn Regex & its special characters. This is what I came up with & it works.
Thx thou....

^[0-9]*(\.?[0-9]{2})$
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.