Regular Expression to validate 14 digit number using C# and VB.Net in ASP.Net

Last Reply 26 days ago By dharmendr

Posted 26 days ago

I have textbox where user enters date and time.

Reading date and time : 20190704(invalid) ----> Wrong Format

Reading date and time: 20190704000000(valid) ----> Correct Format

(Format-: 20190704000000, Maximum 14 charater without space).

The entered characters length should be 14. If legth is 14 then valid otherwise invalid

 

You are viewing reply posted by: dharmendr 26 days ago.
Posted 26 days ago

Hi chetan,

Check this example. Now please take its reference and correct your code.

HTML

<asp:TextBox runat="server" ID="txtValue" />
<asp:Button Text="Validate" runat="server" OnClick="ValidateText" />

C#

protected void ValidateText(object sender, EventArgs e)
{
    System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("^[0-9]{14}$");
    if (regex.IsMatch(txtValue.Text.Trim()))
    {
        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Valid')", true);
    }
    else
    {
        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Invalid')", true);
    }
}

VB.Net

Protected Sub ValidateText(ByVal sender As Object, ByVal e As EventArgs)
    Dim regex As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("^[0-9]{14}$")
    If regex.IsMatch(txtValue.Text.Trim()) Then
        ClientScript.RegisterClientScriptBlock(Me.[GetType](), "", "alert('Valid')", True)
    Else
        ClientScript.RegisterClientScriptBlock(Me.[GetType](), "", "alert('Invalid')", True)
    End If
End Sub

Screenshot