HTML 5 and Bootstrap Jquery Form Validation Plugin

HTML-5-and-Bootstrap-Jquery-Form-Validation-Plugin

jbvalidator is a new jQuery-based form validation plugin built for the latest Bootstrap 5 framework and supports both client-side and server validation.

HTML Data Attributes:

  • Html 5 validation
  • data-v-equal: id of the element that should be the same
  • data-v-min-select: multiple selectbox, minimum selectable count
  • data-v-max-select: multiple selectbox, maximum selectable count
  • data-checkbox-group: the parent attribute of group checkbox elements data-v-min-select:
  • the parent attribute minimum selectable count data-v-required: parent attribute required
  • data-v-min: alternative of the min attribute, this can be used for attribute type text
  • data-v-max: alternative of the max attribute, this can be used for attribute type text
  • data-v-min-length: alternative of the minlength attribute
  • data-v-max-length: alternative of the maxlength attribute
  • data-v-min-size: the input type file minimum file size (byte)
  • data-v-max-size: the input type file maximum file size (byte)
  • data-v-message: alternative error message.

How to make use of it:

1. Load the newest jQuery JavaScript library and Bootstrap 5 framework within the doc.

<link rel="stylesheet" href="/path/to/cdn/bootstrap.min.css" />
<script src="/path/to/cdn/jquery.min.js"></script>
<script src="/path/to/cdn/bootstrap.min.js"></script>

2. Add the novalidate attribute to the form component and apply validators to form fields utilizing the next HTML data attributes:

<form class="example" novalidate>
  <div class="form-group">
    <label>Email</label>
    <input type="email" class="form-control" placeholder="[email protected]" required>
  </div>
  <div class="form-group">
    <label for="password">Password</label>
    <input type="password" name="password" class="form-control" id="password" title="password" required>
  </div>
  <div class="form-group">
    <label for="password">Confirm Password</label>
    <input name="repassword" type="password" class="form-control" data-v-equal="#password" required>
  </div>
  <div class="form-group">
    <label>URL</label>
    <input type="url" class="form-control" placeholder=" " required>
  </div>
  <div class="form-group">
    <label>Using Regex</label>
    <input type="text" class="form-control" pattern="[0-9]+" title="Only number." required>
  </div>
  <div class="form-group">
    <label>Custom Min/Max Values</label>
    <input type="text" class="form-control" data-v-min="10" data-v-max="100">
  </div>
  <div class="form-group">
    <label>Custom Min/Max Length</label>
    <input type="text" class="form-control" data-v-min-length="5" data-v-max-length="10">
  </div>
  <div class="form-group">
    <label>Multiple Select</label>
    <select class="form-select" multiple data-v-min-select="2" data-v-max-select="3">
      <option selected>Open this select menu</option>
      <option value="1">One</option>
      <option value="2">Two</option>
      <option value="3">Three</option>
      <option value="4">Four</option>
      <option value="5">Five</option>
    </select>
  </div>
  <div class="form-group">
    <label>Textarea</label>
    <textarea class="form-control" minlength="10" maxlength="165"></textarea>
  </div>
  <div class="form-group">
    <div class="form-check">
      <input class="form-check-input" type="checkbox" value="" id="defaultCheck1" >
      <label class="form-check-label" for="defaultCheck1">
      checkbox 1
      </label>
    </div>
    <div class="form-check">
      <input class="form-check-input" type="checkbox" value="" id="defaultCheck2" >
      <label class="form-check-label" for="defaultCheck2">
      checkbox 2
      </label>
    </div>
    <div class="form-check">
      <input class="form-check-input" type="checkbox" value="" id="defaultCheck3" >
      <label class="form-check-label" for="defaultCheck3">
      checkbox 3
      </label>
    </div>
  </div>
  <div class="form-group">
    <label>File Input</label>
    <input type="file" class="form-control" data-v-min-size="400" data-v-max-size="450">
  </div>
  <div class="form-group">
    <label>Date Input</label>
    <input type="date" class="form-control" min="2020-10-20">
  </div>
  <div class="form-group">
    <label>Custom message</label>
    <input type="text" class="form-control" minlength="10" data-v-message="Please enter minimum 10 characters" required>
  </div>
  <div class="form-group">
    <input type="submit" class="btn btn-primary" value="Submit">
  </div>
</form>

3. Activate the form validation plugin and determine the path to the language JSON.

let validator = $('form.example').jbvalidator({
    language: 'dist/lang/en.json'
});

4. Override the default error messages or create your individual locals as you see within the en.json.

{
  "maxValue": "You cannot enter a number greater than %s.",
  "minValue": "Please enter a number greater than %s.",
  "maxLength": "Please use maximum %s character. You are using %s characters.",
  "minLength": "Please use minimum %s character, you are using %s characters.",
  "minSelectOption": "Please select at least %s options.",
  "maxSelectOption": "Please select at most %s options.",
  "groupCheckBox": "Please select at least %s options.",
  "equal": "This field does not match %s",
  "fileMinSize": "File size cannot be less than %s bytes.",
  "fileMaxSize": "File size cannot be more than %s bytes.",
  "number": "Please write a number."
}

5. The examples reveal how you can validate form fields on the server aspect.

$(document).on('submit', '.example', function(){
  $.ajax({
    method:"get",
    url:"test.json",
    data: $(this).serialize(),
    success: function (data){
      if(data.status === 'error') {
        validatorServerSide.errorTrigger($('[name=username]'), data.message);
      }
    }
  })
  return false;
});

6. You may also create your individual validation rules utilizing JavaScript.

validator.validator.custom = function(el, event){
  if($(el).is('[name=password]') && $(el).val().length < 5){
    return 'Your password is too weak.';
  }
}

7. Determine whether or not to use a Success to form fields when legitimate.

let validator = $('form.example').jbvalidator({
    language: 'dist/lang/en.json',
    successClass: true
});

8. Override the default CSS classes.

let validator = $('form.example').jbvalidator({
    validFeedBackClass: 'valid-feedbak',
    invalidFeedBackClass: 'invalid-feedback',
    validClass: 'is-valid',
    invalidClass: 'is-invalid'
});

HTML5 Form Validator For Bootstrap 5, Form Validation Plugin/Github


See Demo And Download

Official Website(emretulek): Click Here

This superior jQuery/javascript plugin is developed by emretulek. For extra Advanced Usage, please go to the official website.

Share