Thursday, 5 November 2015

Validating Currency and Number upto 4 decimals



Question :

I would like to validate whether a string satisfy the following currency format with maximum 4 decimal places and could allow ',' regardless of culture.
Eg:
2,000,000(valid)
200000.0000(valid)
200 (valid)
200.00000(invalid)
How could it be done?


Solution :

I have tried this on a web application :

design page :

<asp:TextBox ID="txtNumber" runat="server"></asp:TextBox>
    <asp:Button ID="btnCheck" runat="server" Text="check" onclick="btnCheck_Click" />
    <asp:Label ID="lblResult" runat="server"></asp:Label>


code page :




protected void btnCheck_Click(object sender, EventArgs e)
{
string NumberEntered = txtNumber.Text;


if (NumberEntered.Contains('.'))
{
string[] newNumber = NumberEntered.Split('.');


if (newNumber.Length == 2)
{
string noOfZeros = newNumber[1];

if (noOfZeros.Length <= 4)
{
lblResult.Text = "Number is accepted";
}
else
{
lblResult.Text = "Number is not accepted";
}
}
}
else
{
lblResult.Text = "Number is accepted";
}

}


No comments:

Post a Comment