This article shows how to apply validation on a TextBox, CheckBox, RadioButtin in ASP.NET C# using jQuery.

Here in this example I am using a registration form and on button click I am validating all the controls. The following is my aspx code:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>Validation Using jQuery</title>
  6. <script src="jquery-1.8.2.js" type="text/javascript"></script>
  7. <script type="text/javascript">
  8. $(function() {
  9. $('#btnSubmit').click(function() {
  10. if ($("#txtName").val().length === 0) {
  11. alert('Please enter name.')
  12. return false;
  13. }
  14. if ($("#txtEmail").val().length === 0) {
  15. alert('Please enter Email.')
  16. return false;
  17. }
  18. var IsGenderSelect = false;
  19. $('.Gender').each(function() {
  20. if ($(this).find('input[type="radio"]:checked').length == 1) {
  21. IsGenderSelect = true;
  22. }
  23. });
  24. if (!IsGenderSelect) {
  25. alert('Please select gender.')
  26. return false;
  27. }
  28. if (!$('#chkTermsConditions').is(':checked')) {
  29. alert('Please accept Terms & Conditions')
  30. return false;
  31. }
  32. })
  33. });
  34. </script>
  35. </head>
  36. <body>
  37. <form id="form1" runat="server">
  38. <div>
  39. <table cellpadding="5" cellspacing="5" width="70%" align="center" style="background-color: SkyBlue;">
  40. <tr>
  41. <td align="center" colspan="2" style="background-color: Green; font-weight: bold;
  42. font-size: 14pt; color: Blue; font-family: Arial;">
  43. Registration Form
  44. </td>
  45. </tr>
  46. <tr>
  47. <td align="right" width="40%">
  48. Name #:
  49. </td>
  50. <td>
  51. <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
  52. </td>
  53. </tr>
  54. <tr>
  55. <td align="right">
  56. Email #:
  57. </td>
  58. <td>
  59. <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
  60. </td>
  61. </tr>
  62. <tr>
  63. <td align="right">
  64. Sex #:
  65. </td>
  66. <td>
  67. <asp:RadioButton ID="rdbMale" runat="server" Text="Male" GroupName="Gender" CssClass="Gender" />
  68. <asp:RadioButton ID="rdbFemale" runat="server" Text="Female" GroupName="Gender" CssClass="Gender" />
  69. </td>
  70. </tr>
  71. <tr>
  72. <td>
  73. <td>
  74. <asp:CheckBox ID="chkTermsConditions" runat="server" Text="Terms & Conditions" />
  75. </td>
  76. </td>
  77. </tr>
  78. <tr>
  79. <td>
  80. </td>
  81. <td>
  82. <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
  83. </td>
  84. </tr>
  85. </table>
  86. </div>
  87. </form>
  88. </body>
  89. </html>
Now Run the application

If you don't enter something into text boxes:

registration form
Image 1

message from webpage
Image 2

If you don't select gender:

save name
Image 3

If you don't accept Terms & Conditions:

save record
Image 4

submit data
Image 5