Class Act

Back to the shelf

HTML · Snippet · ASP.net

HTML 5 Required Argument

An HTML form using the required attribute so the browser prevents submission until all required fields are filled in.

Example

How to use HTML 5 Required Argument:
After the snippet is in your project, call saveForm.
Paste the markup into the page body and open that page.

Code

html
<!--
This is a basic HTML form. Normally a form will submit automatically
when a button of type="submit" is clicked.
In this example we are PREVENTING the automatic submit so we can
validate the form manually with JavaScript.
-->

<form id="myForm">

    <!--
    Text input for a first name.

    "required" is an HTML5 validation attribute.
    The browser will consider this field invalid if it is empty.
    -->
    <input type="text" id="firstname" required>

    <!--
    Email input.

    Because type="email", the browser will validate:
        1. Field is not empty (because of required)
        2. Value looks like a valid email address
    -->
    <input type="email" id="email" required>

    <!--
    IMPORTANT:
    This button is type="button", NOT type="submit".

    type="button"
        -> does NOT submit the form automatically
        -> allows us to run JavaScript first

    If we used type="submit", the browser would attempt to submit
    the form immediately.

    onclick="saveForm()"
        Calls our JavaScript validation function.
    -->
    <button type="button" onclick="saveForm()">Save</button>

</form>

<script>

function saveForm() {

    /*
    Get a reference to the form element from the DOM.

    document.getElementById("myForm")
        searches the page for the element with id="myForm"
    */
    var form = document.getElementById("myForm");
  

    /*
    checkValidity()

    Built-in HTML5 method that checks ALL validation rules inside the form:
        - required fields
        - email formatting
        - number ranges
        - pattern attributes
        - etc.

    Returns:
        true  -> form is valid
        false -> something failed validation
    */
    if (!form.checkValidity()) {

        /*
        reportValidity()

        Displays the browser's built-in validation messages
        and highlights the invalid fields.

        Example messages:
            "Please fill out this field."
            "Please include an '@' in the email address."
        */
        form.reportValidity();
  

        /*
        Stop the function here.
        Prevents any additional code from running.
        */
        return;
  
    }

    /*
    If execution reaches here, the form passed validation.
    In a real application you would probably:

        - send data via AJAX
        - submit the form
        - save to database
        - redirect to another page
    */
    alert("Form is valid!");
  

}

</script>

<!-- ===================================================== -->
<!-- ALTERNATIVE APPROACHES                                -->
<!-- ===================================================== -->

<!--
ALTERNATIVE #1
Check Values Idependently.
-->
 * practiceName must have <input required id="practiceName"/>
 let txt = document.getElementById("practiceName");
 if (!txt.checkValidity()) {
     txt.reportValidity();

     return false;
     // prevents form submission
 }

<!--
ALTERNATIVE #2
Let the browser handle validation automatically.
No JavaScript required.
-->

<!--
<form id="myForm">
    <input type="text" required>
    <input type="email" required>
    <button type="submit">Save</button>
</form>
-->

<!--
Explanation:

type="submit" triggers automatic browser validation.

If invalid:
    browser blocks submission and shows errors

If valid:
    form submits to the server.
-->

<!--
ALTERNATIVE #3
Use a submit button but intercept the submission
with JavaScript.
-->

<!--
<form id="myForm" onsubmit="return saveForm()">

    <input type="text" required>
    <input type="email" required>

    <button type="submit">Save</button>

</form>
-->

<!--
function saveForm() {

    var form = document.getElementById("myForm");
  

    if (!form.checkValidity()) {
        form.reportValidity();
  
        return false;
     // prevents form submission
    }

    return true;
          // allows form to submit
}
-->

<!--
Explanation:

return false
    prevents submission

return true
    allows submission

This pattern is very common when validating before a normal POST.
-->