Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

I have this function:

C#
function myFunction(id)
{
    var x;
    x = document.getElementById(id).value;
    if(isNaN(x) || x == "")
    {
      alert('Problem input');
       return false;
     }
     else
     {
       return true;
     }
 }



I want to pass textbox value to the function for the verification. I tried the following:

ASP.NET
<asp:ImageButton ID="imageButtonView" runat="server" Height="60px" ToolTip="بحث" Width="60px" BorderStyle="Solid" ImageUrl="~/Images/searchButton.jpg" OnClick="imageButtonView_Click" OnClientClick="return myFunction('<%=textBoxCustomerCode.ClientID%>');"/>


but it does not work. Any idea how to pass the value to the function.

Thanks in advance.
Posted

Hi, try to use javacript funcion below:

C#
function myFunction()
{
    var x;
    x = document.getElementById('<%= textBoxCustomerCode.ClientID %>').value;
    if(isNaN(x) || x == "")
    {
      alert('Problem input');
       return false;
     }
     else
     {
       return true;
     }
 }



call the function by code below:


ASP.NET
<asp:ImageButton ID="imageButtonView" runat="server" Height="60px" ToolTip="بحث" Width="60px" BorderStyle="Solid" ImageUrl="~/Images/searchButton.jpg" OnClick="imageButtonView_Click" OnClientClick="return myFunction();"/>
 
Share this answer
 
Comments
Member 9646334 22-Dec-15 5:09am    
This works fine but I want dynamic solution so I can call the function for any textbox validation in the page not only for one!! Can you please suggest?
deepankarbhatnagar 22-Dec-15 6:19am    
If you require dynamic textbox it means you have to validate all the textboxes
If you want to get textbox value using Jquery do like below

JavaScript
var val = $("#txt1").val();


If you want to get textbox value using Javascript do like below

JavaScript
var val = document.getElementById('<%=txt1.ClientID%>').value;
 
Share this answer
 
Comments
Member 9646334 22-Dec-15 4:15am    
I know that this is inside the function but how can I pass it to the function?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900