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

Last Reply 5 months ago By dharmendr

Posted 5 months 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 5 months ago.
Posted 5 months ago

Hi chetan,

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


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


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);
        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Invalid')", true);


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)
        ClientScript.RegisterClientScriptBlock(Me.[GetType](), "", "alert('Invalid')", True)
    End If
End Sub