Hi jmontano,
Check this example. Now please take its reference and correct your code.
HTML
<asp:TextBox runat="server" ID="txtAmount" OnTextChanged="ValidateAmount" AutoPostBack="true" />
<asp:Label ID="lblMessage" runat="server" />
Namespaces
C#
using System.Drawing;
using System.Text.RegularExpressions;
VB.Net
Imports System.Drawing
Imports System.Text.RegularExpressions
Code
C#
protected void ValidateAmount(object sender, EventArgs e)
{
lblMessage.Text = "";
Regex regxWithThousandSeparator = new Regex(@"^(?!0+\.00)(?=.{1,9}(\.|$))(?!0(?!\.))\d{1,3}(,\d{3})*(\.\d{1,2})?$");
Regex regxWithoutThousandSeparator = new Regex(@"^(\d+(\.\d{0,2})?|\.?\d{1,2})$");
if (regxWithThousandSeparator.IsMatch(txtAmount.Text.Trim()) || regxWithoutThousandSeparator.IsMatch(txtAmount.Text.Trim()))
{
lblMessage.Text = "Valid Amount.";
lblMessage.ForeColor = Color.Green;
}
else
{
lblMessage.Text = "Invalid Amount.";
lblMessage.ForeColor = Color.Red;
}
}
VB.Net
Protected Sub ValidateAmount(ByVal sender As Object, ByVal e As EventArgs)
lblMessage.Text = ""
Dim regxWithThousandSeparator As Regex = New Regex("^(?!0+\.00)(?=.{1,9}(\.|$))(?!0(?!\.))\d{1,3}(,\d{3})*(\.\d{1,2})?$")
Dim regxWithoutThousandSeparator As Regex = New Regex("^(\d+(\.\d{0,2})?|\.?\d{1,2})$")
If regxWithThousandSeparator.IsMatch(txtAmount.Text.Trim()) OrElse regxWithoutThousandSeparator.IsMatch(txtAmount.Text.Trim()) Then
lblMessage.Text = "Valid Amount."
lblMessage.ForeColor = Color.Green
Else
lblMessage.Text = "Invalid Amount."
lblMessage.ForeColor = Color.Red
End If
End Sub
Output
Valid Cases
1
1.00
100.25
17,550.25
9,999,999
Invalid Cases
1.
9,999,999.
1,000.256