Validation
It provides valuable, actionable feedback to your users with HTML5 form validation.
Official documentation
For more detailed information and examples, see the official documentation: jQuery Validation .
How it works
Here's how form validation works with MyTravel:
- MyTravel uses third-party jQuery validation plugin.
- HTML form validation is applied via
.u-has-success
and.u-has-error
classes. It applies to<input>
,<select>
, and<textarea>
elements. - MyTravel scopes the
.u-has-success
and.u-has-error
styles to parent element, applied next to the<js-form-message>
.. - All modern browsers support the constraint validation API , a series of JavaScript methods for validating form controls.
With that in mind, consider the following demos for our custom form validation styles, optional server side classes, and browser defaults.
How to use?
Wrap any block in a parent element with an ID or class and add the same ID in the JS init function of the hs.validation.js
plugin.
Copy-paste the following <script>
near the end of your pages under JS Implementing Plugins to enable it.
<script src="../../assets/vendor/jquery-validation/dist/jquery.validate.min.js"></script>
Copy-paste the following <script>
near the end of your pages under JS MyTravel to enable it.
<script src="../../assets/js/components/hs.validation.js"></script>
Copy-paste the init function under JS Plugins Init., before the closing </body>
tag, to enable it.
<script>
$(document).on('ready', function () {
// initialization of form validation
$.HSCore.components.HSValidation.init('.js-validate');
});
</script>
Basic example
<!-- Contacts Form -->
<form class="js-validate">
<div class="row">
<!-- Input -->
<div class="col-sm-6 mb-6">
<div class="js-form-message js-focus-state">
<label class="form-label">
Your name
<span class="text-danger">*</span>
</label>
<input type="text" class="form-control" name="name" placeholder="Jack Wayley" aria-label="Jack Wayley" required
data-msg="Please enter your name."
data-error-class="u-has-error"
data-success-class="u-has-success">
</div>
</div>
<!-- End Input -->
<!-- Input -->
<div class="col-sm-6 mb-6">
<div class="js-form-message js-focus-state">
<label class="form-label">
Your email address
<span class="text-danger">*</span>
</label>
<input type="email" class="form-control" name="email" placeholder="jackwayley@gmail.com" aria-label="jackwayley@gmail.com" required
data-msg="Please enter a valid email address."
data-error-class="u-has-error"
data-success-class="u-has-success">
</div>
</div>
<!-- End Input -->
<div class="w-100"></div>
<!-- Input -->
<div class="col-sm-6 mb-6">
<div class="js-form-message js-focus-state">
<label class="form-label">
Subject
<span class="text-danger">*</span>
</label>
<input type="text" class="form-control" name="subject" placeholder="Web design" aria-label="Web design" required
data-msg="Please enter a subject."
data-error-class="u-has-error"
data-success-class="u-has-success">
</div>
</div>
<!-- End Input -->
<!-- Input -->
<div class="col-sm-6 mb-6">
<div class="js-form-message js-focus-state">
<label class="form-label">
Your Phone Number
<span class="text-danger">*</span>
</label>
<input type="number" class="form-control" name="phone" placeholder="1-800-643-4500" aria-label="1-800-643-4500" required
data-msg="Please enter a valid phone number."
data-error-class="u-has-error"
data-success-class="u-has-success">
</div>
</div>
<!-- End Input -->
</div>
<!-- Input -->
<div class="js-form-message js-focus-state mb-6">
<label class="form-label">
How can we help you?
<span class="text-danger">*</span>
</label>
<div class="input-group">
<textarea class="form-control" rows="4" name="text" placeholder="Hi there, I would like to ..." aria-label="Hi there, I would like to ..." required
data-msg="Please enter a reason."
data-error-class="u-has-error"
data-success-class="u-has-success"></textarea>
</div>
</div>
<!-- End Input -->
<div class="text-center">
<button type="submit" class="btn btn-primary btn-wide transition-3d-hover mb-4">Submit</button>
<p class="small">We'll get back to you in 1-2 business days.</p>
</div>
</form>
<!-- End Contacts Form -->
Callbacks
errorPlacement: function () {} - function
highlight: function () {} - function
unhighlight: function () {} - function
Methods
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-
, as in data-rule-required=""
.
Attribute | Description |
---|---|
|
Determines whether this field is required to fill-up. |
|
If data-rule-email="" is set to true , this field will work as type="email" |
|
Defines the minimum number of characters for a given field. If the entered characters are less, then the plugin will return an error. |
|
Defines the maximum number of characters for a given field. If there are more typed characters, the plugin will return an error. |
|
Defines the field with which the current field should have the same value. |
|
The text of the message when the error is returned. |
|
The message text is returned if the entered data does not correspond to the possible correct email address. |
|
The message text is returned if the characters in the field are less than specified in the data-rule-minlength |
|
The message text is returned if the characters in the field are more than specified in the data-rule-maxlength |
|
The message text is returned if the entered data does not match the data entered in the field whose ID is specified in the value. |
|
Defines the classes that will be added to the error element. |
|
Defines the classes that will be added to the container in which this field is located, when an error is returned. |
|
Defines the classes that will be added to the container in which this field is located, if it is correctly filled. |
|
This function executes the code inside the function body, every time the error of filling the ALL form is returned. |
|
This function executes the code inside the function body, after the form validation, every time the error of filling a SPECIFIC field is returned. |
|
This function executes the code, inside the body of the function, after the form validation, each time when it receives data in the SPECIFIC field corresponding to the rules of this field. |