locked
How to validate company number text field with only numeric values and maximum length of 14 digits when clicking on submit button on ASP.net web page form RRS feed

  • Question

  • User-589242180 posted

    How to validate  company number text field with allows only numeric values and maximum length of 14 digits  when clicking on submit button on ASP.net web page . I have a company number field on web page, when i click on submit button the web page should validate the company number field to check it has only numeric values and maximum length of 14 digits. How can i validate this using ASP.net.

    Saturday, December 26, 2020 5:17 AM

All replies

  • User-1026236167 posted
    according to your description i think your requirement is how to generate unique 14 digits company id so when click 
    on button new uniue id generated check this example

    aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="company.aspx.cs" Inherits="company" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <link href="dist/css/styles.css" rel="stylesheet" /> <link href="dist/css/styles.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/js/all.min.js" crossorigin="anonymous"></script> </head> <body> <form id="form1" runat="server"> <div> <div class="col-lg-3.5"> <div class="form-group"> <span style="color: black;"><b>&nbsp;&nbsp;company Id</b></span><br /> <div class="input-group col-lg-12"><br /> <div class="input-group-prepend"> <span class="input-group-text" id="basic-addon13" > <i style="width:20px;" class="fas fa-user" ></i> </span> </div> <span style="color: black;"> <asp:Label ID="TB_applicant_id" aria-describedby="basic-addon13" runat="server" Width="100%" CssClass="form-control" /></span><br /> </div> </div> </div> <asp:Button ID="BT_submit" runat="server" Text="Submit" CssClass="btn btn-primary" OnClick="BT_submit_Click" /> </div> </form> </body> </html> cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Data.SqlClient; using System.Web.UI; using System.Web.UI.WebControls; using System.Security.Cryptography; using System.Text; public partial class company : System.Web.UI.Page { public string GetUniqueKey(int maxSize, char[] chars) { byte[] data = new byte[1]; RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider(); crypto.GetNonZeroBytes(data); data = new byte[maxSize]; crypto.GetNonZeroBytes(data); StringBuilder result = new StringBuilder(maxSize); foreach (byte b in data) { result.Append(chars[b % (chars.Length - 1)]); } return result.ToString(); } protected void Page_Load(object sender, EventArgs e) { // TB_applicant_id.Text = GetUniqueKey(14, "123456789".ToCharArray()); } protected void BT_submit_Click(object sender, EventArgs e) { TB_applicant_id.Text = GetUniqueKey(14, "123456789".ToCharArray()); } }

    if anything i miss u should reply

    Sunday, December 27, 2020 10:00 AM
  • User518677258 posted

    Are you trying to validate the the company identity before user hits submit button ? Is that because you have form fields values you dont want lost  not lost prior to pushing submit ?  

    Monday, December 28, 2020 4:58 AM
  • User-589242180 posted

    Hi Prabhjot thanks for your response, my requirement is to validate company number text field on my existing web page to check it has only numeric values and max length should be 14, if we give input as alphabets or the length exceed 14 it should not* allow the record to submit

    Monday, December 28, 2020 8:49 AM
  • User-1026236167 posted

    as u said user not allow enter alphabets and more than 14 digits only 14 digits and and less than 14 digits allowed
    check this when u click on button as that condition button will perform his functionality

    aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="company.aspx.cs" Inherits="company" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <link href="dist/css/styles.css" rel="stylesheet" /> <link href="dist/css/styles.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/js/all.min.js" crossorigin="anonymous"></script> <script> function isInputNumber(evt) { var ch = String.fromCharCode(evt.which); if (!(/[0-9]/.test(ch))) { evt.preventDefault(); } } </script> </head> <body> <form id="form1" runat="server"> <div> <div class="col-lg-3.5"> <div class="form-group"> <span style="color: black;"><b>&nbsp;&nbsp;company Id</b></span><br /> <div class="input-group col-lg-12"><br /> <div class="input-group-prepend"> <span class="input-group-text" id="basic-addon13" > <i style="width:20px;" class="fas fa-user" ></i> </span> </div> <asp:TextBox ID="TB_applicant_id" runat="server" CssClass="form-control" AutoCompleteType="Disabled" placeholder="enter 14 digits" MaxLength="14" onkeypress="isInputNumber(event)" /> </div> </div> </div> <asp:Button ID="BT_submit" runat="server" Text="Submit" CssClass="btn btn-primary" OnClick="BT_submit_Click" MaxLength="14" onkeypress="isInputNumber(event)" /> </div> </form> </body> </html> cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Data.SqlClient; using System.Web.UI; using System.Web.UI.WebControls; using System.Security.Cryptography; using System.Text; public partial class company : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void BT_submit_Click(object sender, EventArgs e) { } }

    if i could miss  something then reply me

    Monday, December 28, 2020 11:53 AM
  • User379720387 posted

    regex is the way to go

    using System.Text.RegularExpressions

    https://www.c-sharpcorner.com/article/c-sharp-regex-examples/

    Monday, December 28, 2020 6:43 PM