I am trying to write a regular expression to cover US currency, where the
dollar sign and cents are optional. The following string worked when I used
it in the ValidationExpression field of a RegularExpressionValidator
control:
[$]?\d{1,10}([.]\d{2})?
However, when I tried to make a function using this expression, it would not
validate properly, allowing as many numbers after the decimal point as
desired, and also allowing more than 10 numbers for dollars. For now, I've
come up with the following that does validate properly, included in the
function I'm trying to write below:
public static bool isMoney(string pValue)
{
Regex lReg;
lReg = new Regex("\\b[$]?\\d{1,10}[.]\\d{2}\\b");
if (pValue.Trim().Length==0)
{
return true;
}
return lReg.IsMatch(pValue);
}
That will validate the dollar sign and dollar-number range (1-10 digits),
but when I tried to make the cents portion optional, all of the validation
stopped working properly. I also had to add the \b characters to encapsulate
the string, again so it would work properly.
Any assistance in fixing this up would be appreciated. Thanks.
--
Kenneth S. McAndrew
Software Developer, Information Concepts
km*******@infoconcepts.com