Validation on Textboxes - Problems when pressing "enter"

Last post 08-28-2008 3:13 PM by Zenthil. 14 replies.

Sort Posts:

  • Validation on Textboxes - Problems when pressing "enter"

    08-04-2006, 1:34 PM
    • Loading...
    • FatMike206
    • Joined on 05-11-2006, 5:49 PM
    • Posts 13

    Hello everyone!

    I have the followong problem and I would aprecciate any kind of help or suggestion on how to solve it!

    I have a page with 3 dropdownlists along with one button, and a textbox along with another button. There are to possible actions in this page, select something from the textboxes and submit, or write something in the textbox and submit!

    If the user chooses the DropDown action, The selected indexes of the Drop Downs cannot be zero, so I associated requiredFieldValidators with each DropDow, created a validation group, associated eachj drop down with it, along with the button of the drop downs an the requiredFieldValidators of the dropdowns. If the user chooses the textbox action, the text in the text box cannot be null, so I associated the text box with another required field validator, created another validation group, associated the text box, the text box button and the text box required field validator with it.

    Everything went sweet when I click any of the buttons! However, when I enter text in the textbox and press the "Enter" key, the page posts back and executes the drop down validation!! Weird, no? Anyone has any idea why this happens, or how to execute the action that I want when the users presses enter(wich is the text box action)?

    Thanks in advance!!

  • Re: Validation on Textboxes - Problems when pressing "enter"

    08-04-2006, 11:51 PM
    • Loading...
    • LudovicoVan
    • Joined on 12-02-2004, 3:01 PM
    • Hic abundant leones!
    • Posts 1,714

    Hello Mike,

    some browsers post the form when there's an unique textbox into the form and user hits enter in it, sort of legacy behaviour from the old days of html page programming.

    To work around it, just try the following:

       <form ... onsubmit="return false">

    Btw, you can find solutions around handling key events for the textbox, but that's just more and unneeded work...

    HTH. -LV

    Julio Di Egidio
    Analyst Programmer
    http://julio.diegidio.name

    (Peace X Love] = [++1)
  • Re: Validation on Textboxes - Problems when pressing "enter"

    12-18-2006, 8:39 PM
    • Loading...
    • digitaldoom
    • Joined on 11-11-2002, 9:54 AM
    • Moreno Valley
    • Posts 28

    doing this causes enter to no longer work in the following browsers:

    ie7, ff2.0

  • Re: Validation on Textboxes - Problems when pressing "enter"

    01-17-2007, 11:32 AM
    • Loading...
    • JoshStodola
    • Joined on 01-16-2007, 2:17 PM
    • Heartland of America
    • Posts 3,162

    The solution is simple, and yet unexplainable.  In Internet Explorer if you only have one textbox within a <form> tag, for some extremely stupid reason it will not fire the submit buttons "Click" event upon pressing Enter, but it will, however, continue to submit the form.  Don't ask me why it does this, there are a million things I ask "Why" when it comes to IE.  But, I have learned to just live with them.

    The solution is to fake out the browser by making it think there is another textbox there.  This is what I did, directly above the Submit button of the form:

    <asp:TextBox ID="TextBox1" runat="server" Style="display:none; visibility:hidden;"></asp:TextBox>

    You cannot set the Visible="False" becuase then the control would not get rendered and you will have the same problems.  I did this, and now pressing the enter button executes code behind the buttons click event

    Hope this helps :^)

    -Josh Geeked

    Josh Stodola ← Come check out my blog!
  • Re: Validation on Textboxes - Problems when pressing "enter"

    05-24-2007, 12:46 PM
    • Loading...
    • johnsjr
    • Joined on 05-24-2007, 4:41 PM
    • Posts 2

     NICE!  I've been looking for an easy solution to this. I had no idea it was specific to IE. Thanks!

  • Re: Validation on Textboxes - Problems when pressing "enter"

    05-24-2007, 1:12 PM
    • Loading...
    • JoshStodola
    • Joined on 01-16-2007, 2:17 PM
    • Heartland of America
    • Posts 3,162

    Hey, great!  I am glad someone got use out of this - the original starter of this thread never got back to me.

    Glad you found a solution Wink

    Josh Stodola ← Come check out my blog!
  • Re: Validation on Textboxes - Problems when pressing "enter"

    07-09-2007, 3:36 AM
    • Loading...
    • Girya
    • Joined on 06-25-2007, 3:13 PM
    • Posts 4

    Hey Josh,

    I have a similar problem....!

    There is one basic search..... with one textbox and one "search" button.

    There is one more panel for advanced search containing

    3 textbox 1 checkbox and another search button with a cancel button (a image button)

    now the for basic search it works perfectly fine.

    but for the advanced search its giving problems.Instead of trigerring the search button it triggers the cancel button that appears before

    the search button.Please help me.

    Thanks...

    With regards

    Girish P.

     

     

  • Re: Validation on Textboxes - Problems when pressing "enter"

    07-11-2007, 5:27 PM
    • Loading...
    • m_karbala
    • Joined on 06-28-2007, 9:19 PM
    • Posts 113

    You can use DefaultButton attribute for form or panel that you have your textbox to be able to set the action after "Enter"

    For example:
    <form id="form1" runat="server" defaultbutton="Button1">
       <div>
          <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
          <br />
          <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
       </
    div>
    </form>

    Maysam K Dabbagh
  • Re: Validation on Textboxes - Problems when pressing "enter"

    08-16-2007, 3:42 PM
    • Loading...
    • krutika
    • Joined on 12-20-2006, 8:26 PM
    • Posts 69

    I have a webform Login page which inherits from a Master page.
    Web form contains 3 textboxes, 2 image buttons and validation controls for the textboxes.
    The form does not get submitted when ‘Enter’ key is pressed from any of the textboxes.

    I tried adding a HTML textbox on the page, but still the form does not get submitted.

    I added asp textbox and set its text and style:hide, still it didnt submit.

    I cant set defaultbutton either as the form tag is in the master page. So I added a panle around it and set its defaultbutton to submitbutton. This way, the form gets submitted, but the validation controls wont get fired. The result is unpredictable. Validation may or may not get triggered.

    What else should I do to make this work?

  • Re: Validation on Textboxes - Problems when pressing "enter"

    11-02-2007, 4:58 AM
    • Loading...
    • Mydonose
    • Joined on 11-02-2007, 8:45 AM
    • Posts 1

    thanks man

    that works fine for me

  • Re: Validation on Textboxes - Problems when pressing "enter"

    02-09-2008, 7:00 AM
    • Loading...
    • dinuselva
    • Joined on 01-09-2008, 11:25 AM
    • kumar
    • Posts 8
    Me too had the same problem once. try

    onkeypress="if(event.keyCode==13) return false;" on textbox attribute.

    kindly
    dinu selva
    chennai
  • Re: Validation on Textboxes - Problems when pressing "enter"

    04-04-2008, 1:18 PM
    • Loading...
    • in2minds
    • Joined on 11-13-2007, 9:11 PM
    • Posts 52

    Thanks for the easy solution, but i am still trying to figure out why the button fires when we add the hidden textbox.

    Mehdi

  • Indifferent [:|] Re: Validation on Textboxes - Problems when pressing "enter"

    08-27-2008, 2:47 PM
    • Loading...
    • Zenthil
    • Joined on 07-26-2008, 6:55 PM
    • Miami
    • Posts 19

    Yes. This will work if you set up the default button on the form. However,  how do you set up default button if you are in the content place holder page?

    example: If you are using master page.

    Please remember to click "Mark as Answer" on this post if it helped you.
  • Re: Validation on Textboxes - Problems when pressing "enter"

    08-27-2008, 3:15 PM
    • Loading...
    • m_karbala
    • Joined on 06-28-2007, 9:19 PM
    • Posts 113
    You can have your controls in a panel and add defaultbutton attributes to the your panel.
    Maysam K Dabbagh
  • Re: Validation on Textboxes - Problems when pressing "enter"

    08-28-2008, 3:13 PM
    • Loading...
    • Zenthil
    • Joined on 07-26-2008, 6:55 PM
    • Miami
    • Posts 19

    Cool. I got it. Thanks m_Karbala.

    Please remember to click "Mark as Answer" on this post if it helped you.
Page 1 of 1 (15 items)