How to validate a form with Javascript and jQuery?

Estrada Web Group
How to validate a form with Javascript and jQuery?

How to validate a form with Javascript and jQuery?

When we have a form that must be completed by the user it is necessary to guarantee that the information that the user captures is clean, correct and useful. The typical fields that we must validate are:

  • An email validation
  • Required field validation
  • Date validation
  • Website URL validation
  • File upload validation
  • Dropdown validation


The following example validates a form using JavaScript and jQuery, I will show you how to create the validations in javascript and jQuery sending a error messages. Find the source code below:

<!DOCTYPE html>
<html lang="en">
    <title>Form validation in javascript with example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link rel="stylesheet" type="text/css" href="css/style.css">
    <link rel="stylesheet" type="text/css" href="">

    <script type="text/javascript" src=""></script>
    <script type="text/javascript" src="js/jquery.validate.min.js"></script>
    <script type="text/javascript" src="js/additional-methods.min.js"></script>

    <script type="text/javascript">
        jQuery(document).ready(function ($) {
                errorClass: "state-error",
                validClass: "state-success",
                errorElement: "em",
                highlight: function (element, errorClass, validClass) {
                unhighlight: function (element, errorClass, validClass) {
                errorPlacement: function (error, element) {
                    if (":radio") ||":checkbox")) {
                    } else {


<body class="woodbg">

    <div class="smart-wrap">
        <div class="smart-forms smart-container wrap-2">

            <div class="form-header header-primary">
                <h4><i class="fa fa-pencil-square"></i>Validar Form con JavaScript y jQuery</h4>
            </div><!-- end .form-header section -->

            <form method="post" id="smart-form">
                <div class="form-body">

                    <div class="frm-row">
                        <div class="section colm colm6">
                            <label for="firstname" class="field prepend-icon">
                                <input type="text" name="firstname" id="firstname" class="gui-input"
                                       data-msg-required="First name is required"
                                       data-msg-letterswithbasicpunc="Please enter letters only"
                                <span class="field-icon"><i class="fa fa-user"></i></span>
                        </div><!-- end section -->

                        <div class="section colm colm6">
                            <label for="lastname" class="field prepend-icon">
                                <input type="text" name="lastname" id="lastname" class="gui-input"
                                       data-msg-required="Last name is required"
                                       data-msg-letterswithbasicpunc="Please enter letters only"
                                <span class="field-icon"><i class="fa fa-user"></i></span>
                        </div><!-- end section -->
                    </div><!-- end .frm-row section -->

                    <div class="section">
                        <label for="useremail" class="field prepend-icon">
                            <input type="email" name="useremail" id="useremail" class="gui-input"
                                   data-msg-required="Enter email address"
                                   data-msg-email="Enter a VALID email address"
                                   placeholder="Nombre de usuario">
                            <span class="field-icon"><i class="fa fa-envelope"></i></span>
                    </div><!-- end section -->

                    <div class="section">
                        <label for="website" class="field prepend-icon">
                            <input type="url" name="website" id="website" class="gui-input"
                                   data-msg-required="Enter website url"
                                   data-msg-url="Enter VALID url"
                                   placeholder="Web site">
                            <span class="field-icon"><i class="fa fa-globe"></i></span>
                    </div><!-- end section -->

                    <div class="section">
                        <label class="field select">
                            <select id="language" name="language"
                                    data-msg-required="Select a language">
                                <option value="">Lenguaje...</option>
                                <option value="EN">English</option>
                                <option value="FR">French</option>
                                <option value="SP">Spanish</option>
                                <option value="CH">Chinese</option>
                                <option value="JP">Japanese</option>
                            <i class="arrow double"></i>
                    </div><!-- end section -->

                    <div class="section">
                        <label for="file1" class="field file">
                            <span class="button btn-primary"> Choose File </span>
                            <input type="file" class="gui-file" name="upload1" id="file1" onChange="document.getElementById('uploader1').value = this.value;"
                                   data-msg-required="Please upload a file"
                                   data-msg-accept="Only image files allowed"
                                   data-msg-filesize="Please upload 10MB or less">
                            <input type="text" class="gui-input" id="uploader1" placeholder="Seleccionar archivo" readonly>
                    </div><!-- end  section -->

                    <div class="section">
                        <label class="field select-multiple">
                            <select name="mobileos" id="mobileos"
                                    data-msg-required="Select an OS" multiple>
                                <option value="0">Windows 10 </option>
                                <option value="1">Windows 7 </option>
                                <option value="2">Google Android</option>
                                <option value="3">Apple iOS</option>                                
                    </div><!-- end  section -->

                    <div class="section">
                        <label for="comment" class="field prepend-icon">
                            <textarea class="gui-textarea" id="comment" name="comment"
                                      data-msg-required="Please enter your comments"
                            <span class="field-icon"><i class="fa fa-comments"></i></span>
                            <span class="input-hint">
                                <strong>DO NOT:</strong> Be negative or off topic, we expect a great comment...
                    </div><!-- end section -->

                </div><!-- end .form-body section -->
                <div class="form-footer">
                    <button type="submit" class="button btn-primary"> Validate Form </button>
                    <button type="reset" class="button"> Cancel </button>
                </div><!-- end .form-footer section -->

        </div><!-- end .smart-forms section -->
    </div><!-- end .smart-wrap section -->

    <div></div><!-- end section -->


What did you think of the example?
I hope you liked and served this article based on how to validate a form in JavaScript and jQuery with error messages. I would like to receive your comments and suggestions. Your comments are very valuable to improve every day, questions or comments about this article are always welcome.

Download example

Compartir artículo:

Más artículos geniales

Manténgase actualizado

Obtenga excelente contenido en su bandeja de entrada todas las semanas.
Solo contenido excelente, no compartimos su correo electrónico con terceros.
Subir al inicio de la pantalla