title: Validate form input fields using jquery
body: <ol>
  <li><p>I'm working on form validation. Everything is working fine what I actually want is I want to add some more input fields like checkbox, radio button, select and textarea, upload file and like so into the form I want them to be validated as well.</p></li>
  <li><p>I got the email input error working but it is not working correctly as it should validate the email first and then it should remove the error message but it is removing the error message just after entering few characters.</p></li>
  <li><p>I want the phone number to be validated. Like the user should enter 10 numeric digits that is in India if in another country that will differ I am a bit confused how to do it.</p></li>
  <li><p>I want a success message to pop up when all the fields are correctly validated as they should. what I tried is this: </p>
  
  <pre><code>$('.success_msg').fadeIn().delay(3000).fadeOut();
  $('input , textarea , select').val('').removeClass('valid');
  event.preventDefault();
  </code></pre></li>
  <li><p>I want all the fields to be cleared when all the validations are done and the success message is sent.</p></li>
  </ol>
  
  <p>Can anyone point me in the right direction?</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var Validator = function(formObject) {
    this.form = $(formObject);
  
    var Elements = {
      name: {
        reg: /^[a-zA-Z]{2,20}$/,
        error: "Not a valid name.",
      },
  
      email: {
        reg: /^[a-z-0-9_+.-]+\@([a-z0-9-]+\.)+[a-z0-9]{2,7}$/i,
        error: "Not a valid e-mail address.",
      },
      phone: {
        reg: /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/,
        error: "Not a valid number.",
      },
  
      message: {
        reg: /^(?!\s*$).+/,
        error: "Message field cannot be empty.",
      },
    };
  
    var handleError = function(element, message) {
      element.addClass('input-error');
      var $err_msg = element.parent('div');
      $err_msg.find('.error').remove();
      var error = $('&lt;div class="error"&gt;&lt;/div&gt;').text(message);
      error.appendTo($err_msg);
      element.keyup(function() {
        $(error).fadeOut(1000, function() {
          element.removeClass('input-error');
        });
      });
  
    };
  
    this.validate = function() {
      var errorCount = 0;
      this.form.find("input, textarea").each(function(index, field) {
        var type = $(field).data("validation");
        var validation = Elements[type];
        if (validation) {
          if (!validation.reg.test($(field).val())) {
            errorCount++;
            handleError($(field), validation.error);
          }
        }
      })
      return errorCount == 0;
    };
  };
  
  $(function() {
    $("form#test").on("submit", function(event) {
      //event.preventDefault();
      return new Validator(this).validate(); // "this" here refers to the form
  
    })
  })</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>body {
    background: #fff;
    color: #333;
    font: 76% Verdana, sans-serif;
  }
  
  form {
    margin: 1em 0 0 2em;
    width: 90%;
  }
  
  fieldset {
    margin: 0;
    border: 1px solid #ccc;
    padding-bottom: 1em;
  }
  
  legend {
    font-weight: bold;
    text-transform: uppercase;
  }
  
  label {
    float: left;
    width: 5em;
    padding-right: 2em;
    font-weight: bold;
  }
  
  div {
    margin-bottom: 30px;
  }
  
  input {
    font: 1em Verdana, sans-serif;
  }
  
  fieldset ul li input {
    float: left;
    width: 120px;
    border: 1px solid #ccc;
  }
  
  textarea {
    width: 300px;
    height: 200px;
    border: 1px solid #ccc;
    font: 1em Verdana, sans-serif;
  }
  
  form p {
    margin: 0;
    padding: 0.4em 0 0 7em;
  }
  
  form p input {
    background: #666;
    color: #fff;
    font-weight: bold;
  }
  
  div.error {
    clear: left;
    margin-left: 5.3em;
    color: red;
    padding-right: 1.3em;
    height: 100%;
    padding-bottom: 0.3em;
    line-height: 1.3;
  }
  
  .input-error {
    background: #ff9;
    border: 1px solid red;
  }
  
  .success_msg {
    width: 350px;
    line-height: 40px;
    border: 1px solid green;
    border-radius: 5px;
    background-color: rgba(213, 255, 187, 0.7);
    display: none;
    position: absolute;
    bottom: 5px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 999;
    text-align: center;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;form action="#" method="post" id="test"&gt;
  
    &lt;fieldset&gt;
  
      &lt;legend&gt;Contact information&lt;/legend&gt;
  
  
      &lt;div&gt;
        &lt;label for="firstname"&gt;Firstname:&lt;/label&gt;
        &lt;input type="text" name="firstname" id="firstname" data-validation="name" /&gt;
      &lt;/div&gt;
  
  
      &lt;div&gt;
        &lt;label for="lastname"&gt;Lastname:&lt;/label&gt;
        &lt;input type="text" name="lastname" id="lastname" data-validation="name" /&gt;
      &lt;/div&gt;
  
      &lt;div&gt;
        &lt;label for="email"&gt;Email:&lt;/label&gt;
        &lt;input type="email" name="email" id="email" data-validation="email" /&gt;
  
      &lt;/div&gt;
      &lt;div&gt;
        &lt;label for="phone"&gt;phone&lt;/label&gt;
        &lt;input type="number" name="phone" id="phone" data-validation="phone" /&gt;
      &lt;/div&gt;
  
      &lt;div&gt;
        &lt;label&gt;Gender:&lt;/label&gt;
        &lt;input type="radio" name="gender" value="male" data-validation="gender" /&gt;
        &lt;input type="radio" name="gender" value="female" data-validation="gender"&gt;
      &lt;/div&gt;
  
      &lt;div&gt;
        &lt;label&gt;select&lt;/label&gt;
        &lt;input type="checkbox" name="checkbox" id="checkbox1" value="demo1" data-validation="checkbox" /&gt;
        &lt;input type="checkbox" name="checkbox" id="checkbox2" value="demo2" data-validation="checkbox" /&gt;
        &lt;input type="checkbox" name="checkbox" id="checkbox3" value="demo3" ata-validation="checkbox" /&gt;
      &lt;/div&gt;
  
      &lt;select data-validation="selectOption"&gt;
        &lt;option value=""&gt;Select any option&lt;/option&gt;
        &lt;option value="red"&gt;Red&lt;/option&gt;
        &lt;option value="blue"&gt;Blue&lt;/option&gt;
        &lt;option value="green"&gt;Green&lt;/option&gt;
      &lt;/select&gt;
  
      &lt;div&gt;
        &lt;label&gt;Upload:&lt;/label&gt;
        &lt;input type="file" name="file" id="file" data-validation="file" /&gt;
      &lt;/div&gt;
  
      &lt;div&gt;
        &lt;label for="message"&gt;Message:&lt;/label&gt;
        &lt;textarea id="message" name="message" cols="30" rows="15" data-validation="message"&gt;&lt;/textarea&gt;
      &lt;/div&gt;
  
      &lt;p&gt;&lt;input type="submit" name="send" id="send" value="Send" /&gt;&lt;/p&gt;
  
    &lt;/fieldset&gt;
    &lt;div class="success_msg"&gt;
      &lt;p&gt;Form submitted Successfully&lt;/p&gt;
    &lt;/div&gt;
  &lt;/form&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Please feel free to clear your doubts before you invest your time answering the question.</p>