jquery.validateWrapper

Wrapper plugin for JQuery Validate Plugin.

View on GitHub

jquery.validateWrapper v1.19.1 CodeFactor

This validateWrapper plugin will run above the JQuery Validate Plugin.

Basically, the validateWrapper plugin will help you to validate form elements by adding a few lines of code. Just use the form selector and call the validateWrapper plugin and the form will start validating. This will help the developers to maintain a single file for all the form. It will also help the developers in terms of code redundancy, latency, and many other aspects.

Below is the documentation please follow them to get started with Jquery.validateWrapper.


Prerequisites

// required plugins.
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/additional-methods.min.js"></script>
<form action="#" name="demoForm" method="POST" novalidate="novalidate" onsubmit="return false;">

Note: name , novalidate and onsubmit attributes are compulsory.

Documentation

<script src="jquery.validateWrapper.min.js"> </script> //minified
<script src="jquery.validateWrapper.js"> </script> //non minified
$('form').validateWrapper(); //Selector should be always reference to a form element.

This will validate the form elements with default values.

$('form').validateWrapper({
    ignore  : ":hidden:not(.hidden-required)",
});
$('form').validateWrapper({
    errorClass  : 'error-fld',
});
$('form').validateWrapper({
    errorElement  : 'p',
});
$('form').validateWrapper({
    validClass  : 'valid-fld',
});
$('form').validateWrapper({
    unhighlight : function (element, errorClass, validClass) {
        //Your logic comes here.
      },
});
$('form').validateWrapper({
    invalidHandler  : function (form, validator) {
        //Your logic comes here.
      },
});
$('form').validateWrapper({
    errorPlacement  : function (error, element) {
        //Your logic comes here.
      },
});
$('form').validateWrapper({
    normalizer  : function (value) {
    //Your logic comes here.
    },
});
$('form').validateWrapper({
    validateOnLoad    : true
});
$('form').validateWrapper({
    validateOnClick    : true
});
<button type="button" name="Button" class="btn btn-primary _validate_oc">Button</button>
$('form').validateWrapper({
    validateOnKeyPress    : true
});
<input type="email" class="form-control _validate_kp" id="email" name="email"  />
$('form').validateWrapper({
    resetValidator    : true
});
<button type="reset" name="reset" class="btn btn-primary _reset_validator">Reset Button</button>
<button type="button" name="button" class="btn btn-primary _reset_validator">Button</button>
$('form').validateWrapper({
    destroyOnCallback    : true
});
$('form').validateWrapper({
    addToValidator  : function (form,validator) {
        additionalMethods(form,validator);
    },
});

//function name can be anything.
function additionalMethods(form,validator){
    //adding color to the filled element in the form
    $( "input:filled, textarea:filled, select:filled" ).css( "background-color", "#bbbbff");
}
$('form').validateWrapper({
    onComplete  : function (form,event) {
    //Your logic comes here.
    },
});
$('form').validateWrapper({
    messages: {
        required    : "Please don't keep the field empty.", //modifying the message.
        valid_email : "Please enter valid email."  //adding new validator Message for custom validation method.
      },
});

Step 1: Create a groupName in groups and add the fields you want to group together against the groupName.

$('form').validateWrapper({
    groups: {
        g_name_group: "first_name last_name" //g_name_group is a groupName
    }
});

Step 2: Set the groupName as a class into the fields which are grouped together.

<input type="text" class="form-control g_name_group " id="first_name" name="first_name" required="true" />

<input type="text" class="form-control g_name_group " id="last_name" name="last_name" required="true" />

Step 3: Add class group-in-one to all the elements which are grouped together.

<input type="text" class="form-control g_name_group group-in-one" id="first_name" name="first_name" required="true" />

<input type="text" class="form-control g_name_group group-in-one" id="last_name" name="last_name" required="true" />

Step 4: Adding custom messages to each group.

$('form').validateWrapper({
    messages: {
        g_name_group: "Group message"
    }
});

Note: You can have multiple groups with unique groupName. So to keep them unique so we follow a convention to set up groups. So the group name will start with a ‘g_’ followed by group_name.

Step 1: Add class require_from_group to all the elements on which you want to perform require_from_group rules.

<select class="form-control require_from_group" name="day" >
    <option value="">--Please Select--</option>
</select>
<select class="form-control require_from_group" name="month" >
    <option value="">--Please Select--</option>
</select>
<select class="form-control require_from_group" name="year" >
    <option value="">--Please Select--</option>
</select>

Step 2: Create a groupName in require_from_group and add the number of fields you want to validate from the group.

$('form').validateWrapper({
    require_from_group:{
        rfg_dob_group : 2,
    },
});

Step 3: The groupName which is created above, add that groupName as a class for the elements. This will identify each group uniquely.

<select class="form-control rfg_dob_group require_from_group" name="day" >
    <option value="">--Please Select--</option>
</select>
<select class="form-control rfg_dob_group require_from_group" name="month" >
    <option value="">--Please Select--</option>
</select>
<select class="form-control rfg_dob_group require_from_group" name="year" >
    <option value="">--Please Select--</option>
</select>

Step 4: Adding custom messages to each group.

$('form').validateWrapper({
    messages: {
        rfg_dob_group: "Group message"
    }
});

Note: group name has to be unique so we follow a convention to setup require_from_group rule. So the group name will start with an ‘rfg_’ followed by group_name.

$('form').validateWrapper({
    showErrors: {
        "first_name": "First Name should be written in 'Pete Pete' format."
    }
});
<input type="number" class="form-control" id="age" name="age" age="true" />
<textarea name="content" ck-editor="true" id="content" ckeditor_required="true" ></textarea>
<input type="text" class="form-control" hide-validation-message=true id="username" name="username" required="true" />
<input type="text" class="form-control" append-msg-to-parent=true id="username" name="username" required="true" />

Demo URL’s

LICENSE

MIT License

Copyright (c) 2020 Siddhant Naik