When designing web applications, ensuring that user inputs are properly validated is crucial to prevent erroneous data submissions. A typical example is a Contact Us form where users provide sensitive information like their email address and phone number. To ensure data accuracy, we can employ client-side validation techniques. Below is an article that explains how to create a contact form with robust validation for both email and mobile number fields using JavaScript and ASP.NET controls.
Introduction
In this article, we'll walk through the process of validating email and phone number inputs in a Contact Us form. We'll use JavaScript for validation and ASP.NET controls for the form structure. This will ensure that users input valid email addresses and phone numbers before submitting the form.
The form will include two primary input fields:
Email Address
Mobile Number
Both fields will have specific validation rules, such as checking for correct formats, rejecting sequential numbers or letters, and disallowing repeated characters.
HTML Structure: Contact Us Form
Here's the basic structure of the contact form built using ASP.NET controls:
<!-- Contact Us Form -->
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 showblock nopad" runat="server" id="contactus_dis" style="font-size: 21px;">
<asp:Panel ID="panelsigup" runat="server" DefaultButton="btnsignup1">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 nopad">
<div class="footer_title col-xs-12 col-sm-12 col-md-12 col-lg-12 nopad" id="addclass2">
We will share you more information
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 nopad" id="addclass">
<div class="col-md-3 col-sm-3 col-xs-12 nopad">
<asp:TextBox ID="txtemail1" runat="server" placeholder=" My email-id" CssClass="txt_bx tstcc foot_text textmail"
onblur="data_function(this)" MaxLength="60"></asp:TextBox>
</div>
<div class="col-md-2 col-sm-3 col-xs-12 nopad">
<asp:TextBox ID="txtcontactno1" runat="server" placeholder=" My contact no" CssClass="txt_bx tstcc foot_text textno"
onblur="data_function(this)" MaxLength="15" onkeypress="return isNumberKey(event);"></asp:TextBox>
</div>
<div class="col-md-2 col-sm-2 col-xs-12 nopad">
<asp:Button ID="btnsignup1" runat="server" Text="Get Details" CssClass="btn" OnClientClick="return validateForm();"
OnClick="btn1_signup" />
</div>
</div>
</asp:Panel>
</div>
txtemail1– Email input field.txtcontactno1– Mobile number input field.btnsignup1– Submit button with validation.
JavaScript Validation Logic
We'll now explain the JavaScript code that ensures the email and mobile number are entered correctly.
Mobile Number Validation
The validateMobile() function checks the following:
The number must be exactly 10 digits.
It cannot consist of identical digits (like
1111111111).It cannot contain sequential digits (like
1234567890or9876543210).

Join the conversation! Your thoughts help the community grow.