Results 1 to 4 of 4

Thread: Problem of javascript and form with validation key entry

  1. #1
    Join Date
    Aug 2009
    Posts
    124

    Problem of javascript and form with validation key entry

    When the user wants to search, I check if the textbox is empty and also validate its way by clicking on the "search" button, or by validating the "enter" key.

    After the user performs a search with a keyword (found or not found) the 1st time, it tries to validate the text field blank validating the enter key (not clicking on the search button), there is one error message but the page is reloaded! For now, I do a little checking in PHP to determine if the field is empty, so the user gets the message "Please enter your search." on the page.

    My question is: why the page is reloaded it by validating the enter key while the field is empty? Should I completely prohibit the use of the enter key? (a little too simple for my liking!)

    Here is my simplified code:

    HTML Code:
    <script type="text/javascript">
    <!--
    function controle()
    {
    if(document.formsearch.input.value!='')
    {
      document.formsearch.action = "index-search.html";
      document.formsearch.submit();
    }
    else
    {
      alert("Not work!" );
    }
    }
    -->
    </script>
    </head>
    <body>
    <form name="formsearch" method="post">
    <input name="input" id="input" type="text" size="30" onKeyPress="if(event.keyCode == 13) controle();"/>
    <input class="button" type="button" name="test" value="Search" onClick="controle();"></form>
    </body>
    </html>

  2. #2
    Join Date
    Nov 2008
    Posts
    1,192

    Re: Problem of javascript and form with validation key entry

    Your way is just the most disgusting

    HTML Code:
    <script type="text/javascript">
    <!--
    function controle(form)
    {
     if(form.input.value=='') {
     alert("Not work!" );
     return false;
     }
     return true
    }
    -->
    </script>
    </head>
    <body>
    <form method="post" action="index-search.html" onsubmit="return controle(this);">
    <input name="input" id="input" type="text" size="30"/>
    <input class="button" type="submit" name="test" value="Search">
    </form>
    </body>
    </html>
    What have I done? I set the type = "submit" instead of button. Now as soon as you do "Enter", your form gets submit and executes the form.
    Then I transferred all your JS to the other side on the Form and then returns true or false, if I return false, the form will not be sent, if I return true, it will be. Therefore the control function it will return true or false, depending on the value contained in 'input'

  3. #3
    Join Date
    Aug 2009
    Posts
    124

    Re: Problem of javascript and form with validation key entry

    Thank you for your help ...

    Except that I preferred to avoid your comments even if you're right. It is not difficult to explain, however gently, saying to avoid using javascript at all is not? Anyway ...

  4. #4
    Join Date
    Nov 2008
    Posts
    1,192

    Re: Problem of javascript and form with validation key entry

    If I am so direct is that all code, HTML, JavaScript was not very good approach. After yes I am a big bad and I make remarks that are not wrong, but direct.

Similar Threads

  1. How to Submit a Form Using JavaScript?
    By Damien25 in forum Software Development
    Replies: 4
    Last Post: 07-02-2010, 07:05 AM
  2. How to use Form Validation in JavaScript?
    By Dilbert in forum Software Development
    Replies: 4
    Last Post: 30-01-2010, 08:30 PM
  3. Php form with javascript
    By Solaris in forum Software Development
    Replies: 3
    Last Post: 30-11-2009, 02:22 PM
  4. Problem Submit Form in Firefox [javascript]
    By Swetlano in forum Software Development
    Replies: 0
    Last Post: 09-02-2009, 11:07 PM
  5. Disable cell validation on form DataGridView
    By Chandrakant81 in forum Software Development
    Replies: 3
    Last Post: 06-02-2009, 05:52 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,012,061.88996 seconds with 17 queries