Validate text in two text boxes on a single button click

Last post 02-15-2007 9:26 AM by JoshStodola. 7 replies.

Sort Posts:

  • Validate text in two text boxes on a single button click

    12-20-2006, 4:40 PM
    • Loading...
    • granada
    • Joined on 12-20-2006, 9:22 PM
    • Posts 3

    Hi I have two text boxes where user can enter search text. When the user clicks on the search button both the text boxes should not be empty. If the user enters some value in one text box they should be able to search.

    I am trying to use java script for this.

    Here is the code I have written. But nothing happens when both text boxes are empty and user clicks on the Search button. Please help.

    function ValidateDevice()
        {
            var a= document.getElementById("txt1")
           
            var b= document.getElementById("txt2")
           


            if (a.Value) = ""
            {
                if (a.Value) = "" Then
                 {
                    document.write = "Please enter value in one of the text boxes"
                    return false
                 }

    else

    return true

    }

     

    <asp:TextBox ID="txt1" runat="server" </asp:TextBox>
    <asp:TextBox ID="txt2" runat="server" </asp:TextBox>
    <asp:Button ID="btn_search" runat="server" OnClientClick = "ValidateDevice"/>
                       

  • Re: Validate text in two text boxes on a single button click

    12-20-2006, 5:05 PM
    • Loading...
    • rojay12
    • Joined on 04-03-2006, 10:43 PM
    • Sacramento, CA
    • Posts 614

    Your almost there just a few mistakes

     

    function ValidateDevice()
        {
            var a= document.getElementById('txt1');
            var b= document.getElementById('txt2');
            
            if (a.value.length == 0 && b.value.length == 0)
            {
                alert('Please enter value in one of the text boxes');
                return false;
             }
             else
            {
            return true;
            }
    }

    Jared Roberts
    Lead Application Developer
  • Re: Validate text in two text boxes on a single button click

    12-20-2006, 5:07 PM
    • Loading...
    • RTernier
    • Joined on 06-05-2003, 1:40 AM
    • British Columbia, Canada
    • Posts 1,037

    Wouldn't the logic be "or" || ?

     Seeing both have to be filled if A || B are empty then an error needs to happen.

     Edit---

    nm that, my eye's didn't pick up the part.... When the user clicks on the search button both the text boxes should not be empty.

    The Killer Ninja Coding Monkeys thank those that mark helpful posts as answers.

    My Site | My Examples | My Blog
  • Re: Validate text in two text boxes on a single button click

    12-20-2006, 9:56 PM

    I think you have miss used the sign if(a.value=="") you have written single "=" sign while comparing.

    = is assign operator

    == is comparison.

    by mistake you have used = sign.

  • Re: Validate text in two text boxes on a single button click

    12-27-2006, 3:51 PM
    • Loading...
    • granada
    • Joined on 12-20-2006, 9:22 PM
    • Posts 3

    Hello Experts,

     Thanks for your suggestions.

    But my Java script is still not working. Even if both the text boxes are empty , I do not get get alert message.

    I am calling the java script function on t he OnclientClick event of the button.

    <asp:Button ID="btn_search" runat="server" OnClientClick = "ValidateDevice"/>

    I do not understand what's wrong.

    Thanks 

  • Re: Validate text in two text boxes on a single button click

    12-27-2006, 4:06 PM
    • Loading...
    • Rossoneri
    • Joined on 11-09-2006, 8:57 PM
    • O-H I-O
    • Posts 428

    Use rojay's javascript above and terminate the call to the function

    <script type="text/javascript">
    function ValidateDevice()
    {
        var a = document.getElementById("txt1")      
        var b = document.getElementById("txt2")
        if ( a.value.length == 0 && b.value.length == 0 )
        {
            alert('Please enter value in one of the text boxes');
            return false;
        }
        else
        {
            return true;
        }
    }
    </script>
    <body>
        <form id="form1" runat="server">
        <asp:TextBox ID="txt1" runat="server" />
        <asp:TextBox ID="txt2" runat="server" />
        <asp:Button ID="btn_search" runat="server" OnClientClick="ValidateDevice();"/>
        </form>
    </body>

  • Re: Validate text in two text boxes on a single button click

    02-14-2007, 4:27 PM

    function ValidateDevice()
        {
            if (document.getElementById("<%=txt1.ClientID%>").value =="")

              {

               if (document.getElementById("<%=txt2.ClientID%>").value =="")

               {

                 alert('plz enter value in atleast one textbox');

                  return false;

               }

           retrun true;

            }  return true;

    }


            
         

     

    <asp:TextBox ID="txt1" runat="server" </asp:TextBox>
    <asp:TextBox ID="txt2" runat="server" </asp:TextBox>
    <asp:Button ID="btn_search" runat="server" OnClientClick = "return ValidateDevice()"/>

     

    Add button attribute in page load

    btn_search.Attributes.Add("onclientclick","retrun ValidateDevice"();

  • Re: Validate text in two text boxes on a single button click

    02-15-2007, 9:26 AM
    • Loading...
    • JoshStodola
    • Joined on 01-16-2007, 9:17 AM
    • Heartland of America
    • Posts 3,162

    You know, you should never attach javascript form validation to the click event of the submit button.  There are several other ways to submit the form otherwise.  Perhaps pressing the <enter> key????

    Insead of all this onclick junk, put your return ValidateDevice() in the onSubmit event of the <form> tag.  This will handle the form's submission, no matter how it was submitted.  You should do this in the HTML.  And yes, if false is returned, the form will not continue to submit.

    Hope this helps.

    Josh Stodola ← Come check out my blog!
Page 1 of 1 (8 items)
Microsoft Communities
Page view counter