Introduction

This article explains how to validate name, email and phone number in a web page using JavaScript validations in ASP.Net.
In this example, I am using three textboxes. The first TextBox is Name that allows only characters and spaces. The second TextBox is Email that only allows email format. A third TextBox is a phone number, it only allows numbers.
Coding
Validation.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Validation.aspx.cs" Inherits="Validation" %>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head id="Head1" runat="server">
  4. <title>Vallidations Page</title>
  5. <script type="text/javascript">
  6. function Allvalidate()
  7. {
  8. var ValidationSummary = "";
  9. ValidationSummary += NameValidation();
  10. ValidationSummary += EmailValidation();
  11. ValidationSummary += PhonenoValidation();
  12. if (ValidationSummary != "")
  13. {
  14. alert(ValidationSummary);
  15. return false;
  16. }
  17. else
  18. {
  19. alert("Information submited successfuly");
  20. return true;
  21. }
  22. }
  23. function NameValidation()
  24. {
  25. var userid;
  26. var controlId = document.getElementById("<%=txtcontct.ClientID %>");
  27. userid = controlId.value;
  28. var val;
  29. val = /^[0-9]+$/;
  30. var digits = /\d(10)/;
  31. if (userid == "")
  32. {
  33. return ("Please Enter PhoneNo" + "\n");
  34. }
  35. else if (val.test(userid))
  36. {
  37. return "";
  38. }
  39. else
  40. {
  41. return ("PhoneNo should be only in digits" + "\n");
  42. }
  43. }
  44. function EmailValidation()
  45. {
  46. var userid;
  47. var controlId = document.getElementById("<%=txtname.ClientID %>");
  48. userid = controlId.value;
  49. var val = /^[a-zA-Z ]+$/
  50. if (userid == "")
  51. {
  52. return ("Please Enter Name" + "\n");
  53. }
  54. else if (val.test(userid))
  55. {
  56. return "";
  57. }
  58. else
  59. {
  60. return ("Name accepts only spaces and charcters" + "\n");
  61. }
  62. }
  63. function PhonenoValidation()
  64. {
  65. var userid;
  66. var controlId = document.getElementById("<%=txtemail.ClientID %>");
  67. userid = controlId.value;
  68. var val = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
  69. if (userid == "")
  70. {
  71. return ("Please Enter Email Id" + "\n");
  72. }
  73. else if (val.test(userid))
  74. {
  75. return "";
  76. }
  77. else
  78. {
  79. return ("Email should be in this form example: [email protected]" + "\n");
  80. }
  81. }
  82. </script>
  83. </head>
  84. <body>
  85. <form id="form1" runat="server">
  86. <h3 style="color:blue">Validation in JavaScript</h3>
  87. <table style="border-color: #333300; z-index: 1; left: 15px; top: 54px; position: absolute; height: 122px; width: 261px">
  88. <tr>
  89. <td>
  90. <asp:Label ID="lblname" runat="server" Text="FirstName"></asp:Label>
  91. </td>
  92. <td>
  93. <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
  94. </td>
  95. </tr>
  96. <tr>
  97. <td>
  98. <asp:Label ID="lblemail" runat="server" Text="Email"></asp:Label>
  99. </td>
  100. <td>
  101. <asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
  102. </td>
  103. </tr>
  104. <tr>
  105. <td>
  106. <asp:Label ID="lblCntno" runat="server" Text="Phone No"></asp:Label>
  107. </td>
  108. <td>
  109. <asp:TextBox ID="txtcontct" runat="server"></asp:TextBox>
  110. </td>
  111. </tr>
  112. <tr>
  113. <td>
  114. </td>
  115. <td>
  116. <asp:Button ID="bttnsubmit" runat="server" Text="Submit"
  117. OnClientClick ="javascript:Allvalidate()" Font-Bold="True" />
  118. </td>
  119. </tr>
  120. </table>
  121. </h3>
  122. </form>
  123. </body>
  124. </html>
Output 1
We click on the "Submit" button without entering any values in the textboxes:
blank-error.jpg
Output 2
We entered all incorrect values in the textboxes:
enter-wrong-values.jpg
Output 3
We have entered an incorrect value in only the Name TextBox:
Name-error.jpg
Output 4
We have entered an incorrect value in only the Email TextBox:
email-error.jpg
Output 5
We have entered an incorrect value in only the phone number TextBox:
ph-error.jpg
Output 6
We have entered a correct value in all textboxes:
success-msg.jpg
For more information, download the attached sample application.