While working on an ASP.Net project, we often will get a scenario to check the duplicate input value like on the textbox value. We can do it like this.
the page at localhost
  1. <%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
  2. CodeFile="Default3.aspx.cs" Inherits="Default3" %>
  3. <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
  4. <script type="text/javascript">
  5. function testValues() {
  6. var no1 = document.getElementById('<%= TextBox1.ClientID %>').value;
  7. var no2 = document.getElementById('<%= TextBox2.ClientID %>').value;
  8. var no3 = document.getElementById('<%= TextBox3.ClientID %>').value;
  9. var no4 = document.getElementById('<%= TextBox4.ClientID %>').value;
  10. var arrInput = [no1,no2,no3,no4];
  11. var sorted_arr = arrInput.sort();
  12. var results = [];
  13. for (var i = 0; i < arrInput.length - 1; i++) {
  14. if (sorted_arr[i + 1] == sorted_arr[i]) {
  15. results.push(sorted_arr[i]);
  16. }
  17. }
  18. alert("duplicate Value: " + results);
  19. }
  20. </script>
  21. </asp:Content>
  22. <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
  23. <asp:TextBox ID="TextBox1" runat="server" />
  24. <br />
  25. <asp:TextBox ID="TextBox2" runat="server" />
  26. <br />
  27. <asp:TextBox ID="TextBox3" runat="server" />
  28. <br />
  29. <asp:TextBox ID="TextBox4" runat="server" />
  30. <br />
  31. <br />
  32. <asp:Button ID="Button1" Text="Submit" OnClientClick="testValues();" runat="server" />
  33. </asp:Content>

Summary

I have seen so many developers write custom validation logic using C# in a code-behind file. But that is not a good practice since it will degrade the performance of the application. We should write the maximum code on the client-side using JavaScript.
I hope it will be helpful while implementing the custom validation in a web-based project on the client-side.