4

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.

Elijah
  • 13,368
  • 10
  • 57
  • 89
Jason Z
  • 13,122
  • 15
  • 50
  • 62
  • 1
    I ended up figuring out some JavaScript to do what I wanted. Needed to simplify a deployment package and the AJAX toolkit was just another dependency. – Jason Z Oct 14 '08 at 19:41

6 Answers6

10

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

K Paul
  • 134
  • 10
Forgotten Semicolon
  • 13,909
  • 2
  • 51
  • 61
5

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.

Joseph Yaduvanshi
  • 20,241
  • 5
  • 61
  • 69
4

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
  • 11,361
  • 9
  • 58
  • 87
  • 2
    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 Oct 14 '08 at 17:29
2
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); }
sth
  • 222,467
  • 53
  • 283
  • 367
andrew0081
  • 21
  • 1
  • string.Format("{0:c}", sValue) is a nice solution when you need to place the value in a label sometimes and a textbox at other times. – Tony L. May 21 '15 at 17:57
1
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); }
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
0

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
}
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Anjisan
  • 1,789
  • 3
  • 15
  • 26
  • 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 Oct 14 '08 at 18:21