Force ASP.NET textbox to display currency with $ s

2019-01-20 12:44发布

问题:

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:

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



回答2:

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.



回答3:

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?



回答4:

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


回答5:

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


回答6:

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
}