• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 559
  • Last Modified:

Validation on multiple controls with regex

How do I validate multiple textbox's with regex.
Like Validate one textbox for curency & another for AlphaNumeric.
0
u2envy1
Asked:
u2envy1
  • 6
  • 5
1 Solution
 
Anurag ThakurCommented:
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
 
Anurag ThakurCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
u2envy1Author Commented:
It can contain any number 0-9 but must have two decimal places at the end.
9999.99
0
 
Anurag ThakurCommented:
"^\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 ThakurCommented:
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 ThakurCommented:
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 ThakurCommented:
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now