Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Internet & the Web (https://www.askmehelpdesk.com/forumdisplay.php?f=177)
-   -   Validate a form (https://www.askmehelpdesk.com/showthread.php?t=560)

  • Oct 13, 2003, 10:04 AM
    xtazy
    Validate a form
    Can someone send me the java script codes for validating a form when the user has fill it completely. If evrything is validated and the user clicks on the submit button, a new window should open confirming all entries made on the previous form.
    If the user don't fill everything he won't be able load the confirming page.
  • Oct 14, 2003, 04:38 AM
    coreybryant
    Validate a form
    This page should really be helpful:

    http://hotarea.com/fast/display.exe?...alTextBox.html
  • Feb 25, 2004, 07:29 AM
    LTheobald
    Validate a form
    What Corey put would work, but I wouldn't say it's 100% helpful. If you have more than one text box on the screen, the error messages are going to be a little useless. Plus it's for clicking on a link, not submitting a form.

    Here's what I would have. Like in Corey's link, you'll need a piece of JavaScript to validate the form input. I don't have time to write out the code, so here are some pointers in the form of comments...

    Code:

    <script language="javascript">
      function validateForm() {
        // Create a string to hold an error messages
        var errorsFound = "";

        // Check each form element.  If an error is found,
        // add an error message to the error message
        // variable
        if (someErrorTest) {
          errorsFound += "*  The ??? field was blank.\n";
        }

        // Then at the end of the script, check to see if the
        // string you created is still blank.  If it is, there were
        // no errors.
        if (errorsFound == "") {
          // Return true tells the form to move onto the page
          // specified in the action parameter of the form tag
          return true;
        } else {
          // Return false tells the form NOT to move onto the
          // page specified in the action parameter of the form
          //tag
          // Print the error messages to the screen
          alert("Errors Found :\n\n"+ errorsFound);
          return false;
        }
      }
    </script>

    Then when you create your form, use something along the lines of :

    Code:

    <form action="somepage.html" method="post" onsubmit="validateForm()">
    This will call your validate function. If everything went well, true would be return and the form would know that the submit went well, and to move onto the next page. If false was returned, you know there were errors and the page doesn't change.

    I know that's not much help, but it's a starting point. P.s. Sorry about the dodgy code formatting. No idea why it set itself out like that.

  • All times are GMT -7. The time now is 11:23 PM.