Question

Is there a way to get an ASP.NET textbox to accept only currency values, and when the control is validated, insert a $ sign beforehand?

Examples:

10.23 becomes $10.23
$1.45 stays $1.45
10.a raises error due to not being a valid number

I have a RegularExpressionValidator that is verifying the number is valid, but I don't know how to force the $ sign into the text. I suspect JavaScript might work, but was wondering if there was another way to do this.

Was it helpful?

Solution

The ASP.NET MaskedEdit control from the AJAX Control Toolkit can accomplish what you're asking for.

OTHER TIPS

I know an answer has already been accepted, but I wanted to throw out another solution for anyone with the same problem and looking for multiple workarounds.

The way I do this is to use jQuery format currency plugin to bind user input on the client side. Parsing this input on the server side only requires:

// directive
using System.Globalization;

// code
decimal input = -1;
if (decimal.TryParse(txtUserInput.Text, NumberStyles.Currency, 
    CultureInfo.InvariantCulture, out input))
{
    parameter = input.ToString();
}

The only downfall to this is that the user can have javascript turned off, in which case the RegEx validator running server-side would work as a fall-back. If the control is databound, all you have to do is decimalValue.ToString("{0:c}") , as mentioned by others, in order to display the proper currency formatting.

The cool thing about this is that if the user enters the textbox and it shows $0.00 on the client side, the server-side if statement would return false. If your decimal value isn't nullable in the database, just change decimal input = -1 to decimal input = 0 and you'll have a default value of 0.

Another way to do this might be to place the dollar sign outside to the left of the text box. Is there a real need to have the dollar sign inside of the box or will a simple label do?

decimal sValue = decimal.Parse(txtboxValue.Text.Trim());
// Put Code to check whether the $ sign already exist or not.
//Try making a function returning boolean
//if Dollar sign not available do this
{ string LableText = string.Format("{0:c}", sValue); }
else
{ string LableText = Convert.ToString(sValue); }
string sValue = Convert.ToString(txtboxValue.Text.Trim());
// Put Code to check whether the $ sign already exist or not.
//Try making a function returning boolean
//if Dollar sign not available do this
{ string LableText = string.Format("{0:c}", "sValue"); }
else
{ string LableText = Convert.ToString(sValue); }

In the .CS you could do a pattern match along the lines of,

string value = text_box_to_validate.Text;

string myPattern = @"^\$(\d{1,3},?(\d{3},?)*\d{3}(\.\d{0,2})|\d{1,3}(\.\d{2})|\.\d{2})$";
Regex r = new Regex(myPattern);
Match m = r.Match(value);

if (m.Success)
{
    //do something -- everything passed
}
else
{
    //did not match
    //could check if number is good, but is just missing $ in front
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top