How to make fields mandatory in web email form

by 9 replies
12














#programming #email #fields #form #make #mandatory #web
  • do a search on googly for (form validation with javascript)
  • This is a job for JavaScript!

    You can use JavaScript to validate your form entries - make sure that mandatory fields are filled in and also verify that entries like e-mail addresses and phone numbers are formatted correctly.

    You can also use JavaScript to disable the Submit button until the mandatory fields are filled in correctly.

    There are literally thousands of samples of this sort of code available, some that incorporate AJAX techniques to give the user a "Web 2.0" experience.

    Bill
  • What you want to do is create a validate function in Javascript, and add an onSubmit='validate();' attribute on your form.

    The form:

    HTML Code:
    <form onSubmit='validate();' action="somepage.php" method="post'>
       <input type="text" name="firstName" id="firstName">
    </form>
    The Javascript:

    HTML Code:
    <script>
      /* validate function returns true if "ok" false if "bad" */
      validate = function(){
         /* Check if it is blank */
         if (document.getElementById('firstName').value = ''){
           alert('You must enter a First Name!');
           return false;
         }
         return true;
    
      }
    </script>
    if the onSubmit function returns false .. the form will not submit ...
    • [ 1 ] Thanks
    • [1] reply
    • Yes, but he wants it BEFORE submission. I believe you want to look into the field events. <input type="text" OnLoseFocus (???) You need to look at the events that go with the input field.

      Put a red asterisk next to required fields and indicate at the bottom that it's a required field. Because most people WANT to be validated. Hahaha, I know I do!
      • [1] reply
  • Here's a cool Jquery validation library. Click the Demo link.
    bassistance.de jQuery plugin: Validation
  • Thanks for your input, guys.
    I'll try the code, jminkler.

    Best regards,
    Thomas
    • [1] reply
    • I would recommend upgrading to Visual Studio 2010 and working with ASP.NET.
  • If using javascript, is it possible to have it on the page instead of as a popup?

    Best regards,
    Thomas

Next Topics on Trending Feed

  • 12

    Hi, I have some web mail forms in classic asp, where there are some of the fields that are mandatory. At the moment it will be checked on the next page (after clicking the submit button), by simply checking whether the length of the input was >0. If it's not, an error is displayed.