form validation in javascript with example

Keywords | form validation in javascript code, form validation in javascript with example, form validation in javascript, registration form validation in javascript source code

Form Validation in JavaScript with Example

Data validation is the process of ensuring that user input is clean, correct, and useful. Typical validation tasks are:

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

Registration Form Validation in Javascript Source Code

The following form validation in javascript example code will demonstrate you how to form validation in javascript with 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>Form with validation in JavaScript</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"
                                       placeholder="First name...">
                                <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"
                                       placeholder="Last name...">
                                <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="Email address">
                            <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="Current website url">
                            <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="">Select language...</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="no file selected" 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"
                                      placeholder="Your comment">
                            <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 -->

Download Code

What do you think?

I hope you liked this article based on how to form validation in javascript with error messages. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.


Please enter your comment!
Please enter your name here