Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html>
<head>
    <title>jQuery Validation</title>
</head>
<body>
<h2>Form Validate</h2>
<div id="errorBlock"></div>
<form id="createForm" method="post" action="/">
    <div class="control">
        <label for="id">ID</label>
        <input type="text" name="id" id="id"/>
    </div>
    <div class="control">
        <label for="firstName">First Name</label>
        <input type="text" name="firstName" id="firstName"/>
    </div>
    <div class="control">
        <label for="lastName">Last Name</label>
        <input type="text" name="lastName" id="lastName"/>
    </div>
    <div class="control">
        <label for="email">Email</label>
        <input type="text" name="email" id="email" value="info@gmail.com"/>
    </div>
    <div class="control">
        <label for="confirmEmail">Confirm Email</label>
        <input type="text" name="confirmEmail" id="confirmEmail" value="info@gmail.com"/>
    </div>
    <div class="control">
        <label for="tel">Tel</label>
        <input type="text" name="tel" id="tel" value="012-345-678"/>
    </div>
    <div class="control">
        <label for="date">Date</label>
        <input type="text" name="date" id="date"/>
    </div>
    <div class="control">
        <input type="submit" name="submit" id="submit" value="Submit"/>
    </div>
</form>

<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js" type="text/javascript"></script>
<script src="formValidation.js" type="text/javascript"></script>
</body>
</html>
              
            
!

CSS

              
                #errorBlock {
  p {
    label {
      color: red;
    }
  }
}
form {
  div {
    height: 30px;
    label {
      width: 100px;
      display: inline-block;
    }
  }
}

              
            
!

JS

              
                (function ($) {
    $.validator.addMethod("validateTel",
        function (value, element, param) {
            if (!value) {
                this.message = param + " is empty";
                return false;
            }

            if (!value.match(/^[\d]{3}-[\d]{3}-[\d]{3}$/)) {
                this.message = param + " must be format as xxx-xxx-xxx";
                return false;
            }
            return true;
        },
        function () {
            return this.message;
        });

    var validateConfig = {
        onfocusout: false,
        onkeyup: false,
        onclick: false,
        errorLabelContainer: "#errorBlock",
        errorElement: "label",
        wrapper: "p",
        groups: {
            nameGroup: "firstName lastName"
        },
        rules: {
            "id": {
                required: true,
                minlength: 3,
                maxlength: 20,
                number: true
            },
            "firstName": {
                required: true
            },
            "lastName": {
                required: true
            },
            "email": {
                required: true,
                email: true
            },
            "confirmEmail": {
                required: true,
                email: true,
                equalTo: "#email"
            },
            "tel": {
                validateTel: "Telephone"
            }
        },
        messages: {
            "id": {
                required: "ID is required",
                number: "ID must be number"
            },
            "firstName": {
                required: "Name is required"
            },
            "lastName": {
                required: "Name is required"
            },
            "email": {
                required: "Email is required",
                email: "Email is format wrong"
            }
        },
        submitHandler: function (form) {
            alert('Submit successful');
        },
        invalidHandler: function (event, validator) {
            var numberErrors = validator.numberOfInvalids();
            if (numberErrors) {
                alert('Total: ' + numberErrors + ' errors');
            }
        },
        errorPlacement: function (error, element) {
            alert(error[0].innerText);
        }
    };

    var createForm = $('#createForm');
    createForm.on('submit', function (event) {
        event.preventDefault();
        createForm.validate(validateConfig);

        $('#date').rules('add', {
            required: true,
            date: true,
            messages: {
                required: "Date is required",
                date: "Date format is wrong"
            }
        });

        if (createForm.valid()) {
            alert('Send request');
        }
    });

})(jQuery);
              
            
!
999px

Console