Skip to content

Commit 8e50bd7

Browse files
committed
v1.2.0
- Add optional live validation for text-like fields on keyup and select fields on change
1 parent 1975f06 commit 8e50bd7

File tree

13 files changed

+231
-16
lines changed

13 files changed

+231
-16
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Validate.js [![Build Status](https://travis-ci.org/cferdinandi/validate.svg)](https://travis-ci.org/cferdinandi/validate)
22
A lightweight form validation script that augments native HTML5 form validation elements and attributes, providing a better user experience and giving you more control.
33

4-
When a visitor leaves a field, Validate.js immediately validates the field and displays an error if applicable. It also validates the entire form on submit, and provides support for custom `onSubmit()` functions (for example, Ajax form submission).
4+
When a visitor leaves a field, Validate.js immediately validates the field and displays an error if applicable. It also validates the entire form on submit, and provides support for custom `onSubmit()` functions (for example, Ajax form submission). You can pass an option to activate live validation on fields while the visitor is still typing.
55

66
[Download Validate](https://github.com/cferdinandi/validate/archive/master.zip) / [View the demo](http://cferdinandi.github.io/validate/)
77

@@ -152,6 +152,9 @@ validate.init({
152152
messageRangeUnderflow: 'Please select a value that is no less than {min}.', // Displayed with the `mind` attribute is used and the input value is too low
153153
messageGeneric: 'The value you entered for this field is invalid.', // A catchall error, displayed when the field fails validation and none of the other conditions apply
154154

155+
// Live Validation
156+
useLiveValidation: false, // Update errors instantly while the visitor is typing
157+
155158
// Form Submission
156159
disableSubmit: false, // If true, don't submit the form to the server (for Ajax for submission)
157160
onSubmit: function (form, fields) {}, // Function to run if the form successfully validates

dist/js/validate.js

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
2-
* validate v1.1.0: A lightweight form validation script that augments native HTML5 form validation elements and attributes.
3-
* (c) 2017 Chris Ferdinandi
2+
* validate v1.2.0: A lightweight form validation script that augments native HTML5 form validation elements and attributes.
3+
* (c) 2018 Chris Ferdinandi
44
* MIT License
55
* http://github.com/cferdinandi/validate
66
*/
@@ -48,6 +48,9 @@
4848
messageRangeUnderflow: 'Please select a value that is no less than {min}.',
4949
messageGeneric: 'The value you entered for this field is invalid.',
5050

51+
// Live Validation
52+
useLiveValidation: false,
53+
5154
// Form Submission
5255
disableSubmit: false,
5356
onSubmit: function () {},
@@ -406,6 +409,63 @@
406409

407410
};
408411

412+
/**
413+
* Check text field validity when typing
414+
* @private
415+
* @param {Event} event The keyup event
416+
*/
417+
var keyupHandler = function (event) {
418+
419+
// Only run if the field is in a form to be validated
420+
if (!event.target.form || !event.target.form.matches(settings.selector)) return;
421+
422+
// Only run if the field is some kind of text field
423+
var type = event.target.getAttribute('type');
424+
var textLikeInputs = ['text', 'email', 'url', 'tel', 'number', 'password', 'search', 'date', 'time', 'datetime', 'month', 'week'];
425+
if (!(textLikeInputs.indexOf(type) > -1 || event.target.nodeName === 'TEXTAREA')) return;
426+
427+
// Validate the field
428+
var error = validate.hasError(event.target);
429+
430+
// If there's an error, show it
431+
if (error) {
432+
validate.showError(event.target, error);
433+
return;
434+
}
435+
436+
// Otherwise, remove any errors that exist
437+
validate.removeError(event.target);
438+
439+
};
440+
441+
/**
442+
* Check select field validity on change
443+
* @private
444+
* @param {Event} event The change event
445+
*/
446+
var changeHandler = function (event) {
447+
448+
// Only run if the field is in a form to be validated
449+
if (!event.target.form || !event.target.form.matches(settings.selector)) return;
450+
451+
// Only run if the field is a select
452+
var type = event.target.getAttribute('type');
453+
if (!(event.target.nodeName === 'SELECT')) return;
454+
455+
// Validate the field
456+
var error = validate.hasError(event.target);
457+
458+
// If there's an error, show it
459+
if (error) {
460+
validate.showError(event.target, error);
461+
return;
462+
}
463+
464+
// Otherwise, remove any errors that exist
465+
validate.removeError(event.target);
466+
467+
};
468+
409469
/**
410470
* Check all fields on submit
411471
* @private
@@ -462,6 +522,11 @@
462522
document.removeEventListener('click', clickHandler, true);
463523
document.removeEventListener('submit', submitHandler, false);
464524

525+
if (settings.useLiveValidation) {
526+
document.removeEventListener('change', changeHandler, true);
527+
document.removeEventListener('keyup', keyupHandler, true);
528+
}
529+
465530
// Remove all errors
466531
var fields = document.querySelectorAll(settings.errorClass);
467532
for (var i = 0; i < fields.length; i++) {
@@ -500,6 +565,11 @@
500565
document.addEventListener('click', clickHandler, true);
501566
document.addEventListener('submit', submitHandler, false);
502567

568+
if (settings.useLiveValidation) {
569+
document.addEventListener('change', changeHandler, true);
570+
document.addEventListener('keyup', keyupHandler, true);
571+
}
572+
503573
};
504574

505575

dist/js/validate.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/validityState-polyfill.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
2-
* validate v1.1.0: A lightweight form validation script that augments native HTML5 form validation elements and attributes.
3-
* (c) 2017 Chris Ferdinandi
2+
* validate v1.2.0: A lightweight form validation script that augments native HTML5 form validation elements and attributes.
3+
* (c) 2018 Chris Ferdinandi
44
* MIT License
55
* http://github.com/cferdinandi/validate
66
*/

0 commit comments

Comments
 (0)