angus_young_acdc
asked on
Regular Expression for Currency
I am trying to come up with a regular expression to validate some dynamic controls.
I need to be able to validate against certain inputs, such as:
Text box can be empty - this is fine, display no error
Text box can have a whole number e.g. 100 - this is fine, display no error
Text box can have a decimal but must be 2 decimal places maximum (1 is allowed) - display error only if greater than 3 decimal places
Text box should not contain just a decimal point - otherwise display an error
Text box should not contain any other characters than digits and 1 decimal point - otherwise display an error
The above is what I currently have but it doesn't seem to work, on validation it fails for 100, 100.20 etc.
I need to be able to validate against certain inputs, such as:
Text box can be empty - this is fine, display no error
Text box can have a whole number e.g. 100 - this is fine, display no error
Text box can have a decimal but must be 2 decimal places maximum (1 is allowed) - display error only if greater than 3 decimal places
Text box should not contain just a decimal point - otherwise display an error
Text box should not contain any other characters than digits and 1 decimal point - otherwise display an error
isValid = Regex.IsMatch(item.ItemAmount.ToString(), "^[0-9]+(.[0-9]{1,2})?$");
The above is what I currently have but it doesn't seem to work, on validation it fails for 100, 100.20 etc.
ASKER
Hi Kaufmed,
That one kind of works, if I just enter a '.' it doesn't produce an error.
That one kind of works, if I just enter a '.' it doesn't produce an error.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window