views:

3978

answers:

6

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.

+1  A: 

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?

BoltBait
There is no real need for it, I am unfortnately tied to the sales and marketing requirements. I tried arguing against it, but was overruled.
Jason Z
+7  A: 

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

Forgotten Semicolon
A: 

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
        }
Anjisan
Already running that regex. I want the $ sign to be inserted into the textbox value after the value is entered, preferably without a postback.
Jason Z
A: 

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); }

A: 
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); }
andrew0081
+3  A: 

I know an answer has already been accepted, but I wanted to throw out another solutions 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.

Jim Schubert
+1 thanks for plugging my plugin...
bendewey
Sure thing! It's very useful.
Jim Schubert