Checkout for amout (price) in ASP.net
I am using "Sum" as a column in my datatable for my application.
I want to do the following checks on wrt 'Amount' which is a string variable.
1) I want to check if the sum has more than two digits after the decimal point
2) if the amount is positive or negative. (as in the case of an error message with an error)
Can you help with this with short and efficient code snippets ???
EDIT:
@Peter:
But checking the digits after decimal places shows an error even for numbers like 1986, 200134, which are given as input data ...... what to do?
0
source to share
2 answers
I am quickly passing in a validation function that should do what you want.
public static bool validateAmount(string amount)
{
int posDecSep = amount.IndexOf(NumberFormatInfo.CurrentInfo.NumberDecimalSeparator);
int decimalPlaces = amount.Length - posDecSep - 1;
if (posDecSep < 0) { decimalPlaces = 0; }
if (decimalPlaces > 2)
{
//handle error with args, or however your validation works
return false;
}
decimal decAmount;
if (decimal.TryParse(amount, out decAmount))
{
if (decAmount >= 0)
{
//positive
}
else
{
//negative
}
}
return true;
}
+1
source to share
You can create a checked numeric textbox like this:
<asp:TextBox ID="txtDollars" Runat="server" />
<asp:CustomValidator
runat="server"
ErrorMessage="Only a valid number with no more than two decimal places is allowed."
ClientValidationFunction="validateDollars"
/>
<script type=text/javascript>
function validateDollars(source,args)
{
// first check for a valid number
args.IsValid = !isNaN(args.Value)
if(args.IsValid)
{
// confirmed numeric, now check for 3+ decimals.
if(args.Value.Match(/\.\d\d\d+/))
args.IsValid = false;
}
return;
}
</script>
+2
source to share