-1

I have four text boxes in 4 different tabs of an ASP.NET Page. I need to provide same validation message to all the text boxes. Presently I am using four different functions with same functionality. Kindly suggest a way to identify which textbox is being used make that a single funtion

Code from Commend:

<asp:TextBox ID="textBox1" runat="server" ValidationonGroup="abcd"></asp:TextBox>
<asp:CustomValidator ID="ID1" runat="server" ValidationGroup="abcd"
ControlToValidate="textBox1" ErrorMessage="message"
ClientValidationFunction="fname"></asp:CustomValidator>

--Javascript Fn--

function fname(sender, args) { 
 var x = document.getElementById('<%=txtBox1.ClientID%>').value;
 if (some condition) { 
  args.IsValid = false; 
 } else { 
  args.IsValid = true; 
 } 
} 
Daniel Pl.
  • 138
  • 8
Sachin
  • 21
  • 6
  • 1
    can you add the code that you tried – brk Mar 08 '16 at 07:04
  • I added only separate codes for 4 textboxes. I need to identify which textbox is used when click the save button – Sachin Mar 08 '16 at 07:15
  • Well. You can pass your textbox reference in click event to identify which textbox it is. Something like, while calling onclick=`CallMe(this)`; And within function you will have function CallMe(obj). Here you can access obj var as your textbox. – INDIA IT TECH Mar 08 '16 at 07:25
  • My code is below. I have four textboxes and four functions presently. I have to make it one finction --Javascript Fn-- function fname(sender, args) { var x = document.getElementById('<%=txtBox1.ClientID%>').value; if (some condition) { args.IsValid = false; } else { args.IsValid = true; } } – Sachin Mar 08 '16 at 08:35
  • see the above code along with question – Sachin Mar 08 '16 at 09:30

1 Answers1

0

Edit

if you are using Asp.Net then you should use like this

Aspx page code

<div>
        <asp:TextBox runat="server" ID="txt1" ClientIDMode="Static" onKeyPress="javascript:text_changed(this.id);"></asp:TextBox>
    </div>

JS Code

  <script type="text/javascript">
        function text_changed(event)
        {
            var id = event;
            alert(id);
        }
    </script>

You got the textbox id in id variable and now u can get value etc using this id you are applying your conditions.... i hope this will help u

Muhammad Asad
  • 1,772
  • 16
  • 23
  • My code is below. I have four textboxes and four functions presently. I have to make it one finction --Javascript Fn-- function fname(sender, args) { var x = document.getElementById('<%=txtBox1.ClientID%>').value; if (some condition) { args.IsValid = false; } else { args.IsValid = true; } } – Sachin Mar 08 '16 at 08:36