I am looking to validate a text box. The text box is used so that the user can input a clients rate of pay. eg. 8.40, 10.50, 24.80. I want to validate it and I have read that using regular expressions would be the faster and easier way to go. Problem is I am still not familiar with how the characters are being used within the brackets. Any help is greatly appreciated. Thanks!
-
Just curious, but where did you read that regular expressions would be faster?John Rasch– John Rasch2011-03-10 00:27:50 +00:00Commented Mar 10, 2011 at 0:27
-
I guess faster as in, less or shorter code.jquirante– jquirante2011-03-10 00:37:17 +00:00Commented Mar 10, 2011 at 0:37
Add a comment
|
3 Answers
Well, I could ask several questions about what exactly constitutes valid input. But here's an alternate approach:
double dbl;
if (!double.TryParse(Text1.Text, out dbl))
MessageBox.Show("Invalid value entered!");
4 Comments
jquirante
Sorry about that. The textbox is for a persons rate of pay. So any whole number eg. 10, 15 or any number with two decimal places eg. 14.55, 16.65. My first thought was just to check if the characters that the user had entered were all numbers. But then I'd also have to accept any "." characters that were put in.
mikesigs
This is a better solution because you don't have to account for culture (i.e. en-CA = $20.00, en-FR = 20,00$)
John Rasch
While the results would be the same, I would suggest using
decimal instead of double when dealing with money.Jonathan Wood
@John: That's probably a good suggestion, although it ultimately depends on the variable type that will ultimately be used to store the value.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;
public class MainClass{
public static void Main(){
Regex moneyR = new Regex(@"\$(((\d{1,3},)+\d{3})|\d+)\.\d{2}");
string[] money = new string[] { "$0.99",
"$1099999.00",
"$10.25",
"$90,999.99",
"$1,990,999.99",
"$1,999999.99" };
foreach (string m in money){
Console.WriteLine(m);
Console.WriteLine(moneyR.IsMatch(m));
}
}
}
- $0.99 True
- True $1099999.00
- True $10.25
- True $90,999.99
- False $1,990999.99
5 Comments
jquirante
can you explain the characters within the Regex brackets? I don't quite understand why $1,990,999.99 is false.
mikesigs
\$ matches a dollar sign. \d+ matches 1 or more digits. \. matches a single period. \d{2} matches exactly two decimal characters. However, the regex above doesn't allow for commas so I don't see how any of the last three strings would have passed.
jquirante
oh ok. I'm starting to get it now. So to test if the characters pass, I would just use moneyR.IsMatch(txtbox.text); ? And that would either return true or false?
mikesigs
Or you could use a RegularExpressionValidator. I'll post an answer with this.
mikesigs
Seems a bit excessive allowing for the commas and expecting your users to always enter a dollar sign. It's just a pay rate. Sure would like include commas in my pay rate!
<asp:TextBox ID="txtPayRate" runat="server" />
<asp:RegularExpressionValidator
ID="revPayRate"
ControlToValidate="txtPayRate"
ValidationExpression="\d+(\.\d{1,2})?"
ErrorMessage="Message to display in ValidationSummary control"
Text="Message to display in control"
runat="server" >
</asp: RegularExpressionValidator>
The regular expression can be translated as follows:
One or more digits - OR - One or more digits followed by a decimal and one or two digits.
\d+ Any digit, one or more repetitions
(.\d{1,2})? A numbered capture group, zero or one repetitions. Broken down as follows:
\. Literal .
\d{1,2} Any digit, between 1 and 2 repetitions
This will only work for English currencies though. Here's a few examples of what will pass/fail.
- 10.00 PASS
- 1.00 PASS
- 1.1 PASS
- 1 PASS
- 1. FAIL
- 10,00 FAIL
1 Comment
mikesigs
I strongly recommend you download a tool called Expresso. Its very powerful and the full version is free. I've been using it for years. ultrapico.com/Expresso.htm