|
1 | 1 | /*! |
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 |
4 | 4 | * MIT License |
5 | 5 | * http://github.com/cferdinandi/validate |
6 | 6 | */ |
|
48 | 48 | messageRangeUnderflow: 'Please select a value that is no less than {min}.', |
49 | 49 | messageGeneric: 'The value you entered for this field is invalid.', |
50 | 50 |
|
| 51 | +// Live Validation |
| 52 | +useLiveValidation: false, |
| 53 | + |
51 | 54 | // Form Submission |
52 | 55 | disableSubmit: false, |
53 | 56 | onSubmit: function () {}, |
|
406 | 409 |
|
407 | 410 | }; |
408 | 411 |
|
| 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 | + |
409 | 469 | /** |
410 | 470 | * Check all fields on submit |
411 | 471 | * @private |
|
462 | 522 | document.removeEventListener('click', clickHandler, true); |
463 | 523 | document.removeEventListener('submit', submitHandler, false); |
464 | 524 |
|
| 525 | +if (settings.useLiveValidation) { |
| 526 | +document.removeEventListener('change', changeHandler, true); |
| 527 | +document.removeEventListener('keyup', keyupHandler, true); |
| 528 | +} |
| 529 | + |
465 | 530 | // Remove all errors |
466 | 531 | var fields = document.querySelectorAll(settings.errorClass); |
467 | 532 | for (var i = 0; i < fields.length; i++) { |
|
500 | 565 | document.addEventListener('click', clickHandler, true); |
501 | 566 | document.addEventListener('submit', submitHandler, false); |
502 | 567 |
|
| 568 | +if (settings.useLiveValidation) { |
| 569 | +document.addEventListener('change', changeHandler, true); |
| 570 | +document.addEventListener('keyup', keyupHandler, true); |
| 571 | +} |
| 572 | + |
503 | 573 | }; |
504 | 574 |
|
505 | 575 |
|
|
0 commit comments