Skip to content
This repository has been archived by the owner on Feb 10, 2019. It is now read-only.

added min and max validation [Fixes #90] #91

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 38 additions & 22 deletions jquery.h5validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
// User needs help. Enable active validation.
$element.addClass(options.settings.activeClass);

if ($errorID.length) { // These ifs are technically not needed, but improve server-side performance
if ($errorID.length) { // These ifs are technically not needed, but improve server-side performance
if ($element.attr('title')) {
$errorID.text($element.attr('title'));
}
Expand Down Expand Up @@ -154,10 +154,12 @@
rangeOverflow: validity.rangeOverflow || false,
rangeUnderflow: validity.rangeUnderflow || false,
stepMismatch: validity.stepMismatch || false,
tooLong: validity.tooLong || false,
rangeOverflow: validity.rangeOverflow || false,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These were already here. Now we have duplicates. );

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. We need this one. tooLong refers to the character length, not the number range.

rangeUnderflow: validity.rangeUnderflow || false,
tooHigh: validity.tooHigh || false,
typeMismatch: validity.typeMismatch || false,
valid: validity.valid || true,
valueMissing: validity.valueMissing || false
valueMissing: validity.valueMissing || false,
valid: validity.valid || true
}, validity);
},

Expand Down Expand Up @@ -253,13 +255,15 @@
errorIDbare = $this.attr(settings.errorAttribute) || false, // Get the ID of the error element.
errorID = errorIDbare ? '#' + errorIDbare.replace(/(:|\.|\[|\])/g,'\\$1') : false, // Add the hash for convenience. This is done in two steps to avoid two attribute lookups.
required = false,
maxlength,
min,
max,
validity = createValidity({element: this, valid: true}),
$checkRequired = $('<input required>'),
maxlength;
$checkRequired = $('<input required>');

/* If the required attribute exists, set it required to true, unless it's set 'false'.
* This is a minor deviation from the spec, but it seems some browsers have falsey
* required values if the attribute is empty (should be true). The more conformant
* This is a minor deviation from the spec, but it seems some browsers have falsey
* required values if the attribute is empty (should be true). The more conformant
* version of this failed sanity checking in the browser environment.
* This plugin is meant to be practical, not ideologically married to the spec.
*/
Expand All @@ -277,10 +281,22 @@

maxlength = parseInt($this.attr('maxlength'), 10);
if (!isNaN(maxlength) && value.length > maxlength) {
validity.valid = false;
validity.valid = false;
validity.tooLong = true;
}

min = parseInt($this.attr('min'), 10);
if(!isNaN(min) && (value < min)) {
validity.valid = false;
validity.rangeUnderflow = true;
}

max = parseInt($this.attr('max'), 10);
if(!isNaN(max) && (value > max)) {
validity.valid = false;
validity.rangeOverflow = true;
}

if (required && !value) {
validity.valid = false;
validity.valueMissing = true;
Expand Down Expand Up @@ -333,9 +349,9 @@
/**
* Take the event preferences and delegate the events to selected
* objects.
*
*
* @param {object} eventFlags The object containing event flags.
*
*
* @returns {element} The passed element (for method chaining).
*/
delegateEvents: function (selectors, eventFlags, element, settings) {
Expand All @@ -359,10 +375,10 @@
},
/**
* Prepare for event delegation.
*
*
* @param {object} settings The full plugin state, including
* options.
*
* options.
*
* @returns {object} jQuery object for chaining.
*/
bindDelegation: function (settings) {
Expand All @@ -384,7 +400,7 @@
$forms
.attr('novalidate', 'novalidate')
.submit(checkValidityOnSubmitHandler);

$forms.find("input[formnovalidate][type='submit']").click(function(){
$(this).closest("form").unbind('submit', checkValidityOnSubmitHandler);
});
Expand Down Expand Up @@ -418,9 +434,9 @@
* - prevents submission if any invalid fields are found.
* - Optionally validates all fields.
* - Optionally moves focus to the first invalid field.
*
* @param {object} evt The jQuery Event object as from the submit event.
*
*
* @param {object} evt The jQuery Event object as from the submit event.
*
* @returns {object} undefined if no validation was done, true if validation passed, false if validation didn't.
*/
checkValidityOnSubmitHandler = function(evt) {
Expand Down Expand Up @@ -486,10 +502,10 @@
* expressions, and add them to the patternLibrary. Patterns in
* the library are automatically assigned to HTML element pattern
* attributes for validation.
*
*
* @param {Object} patterns A map of pattern names and HTML5 compatible
* regular expressions.
*
*
* @returns {Object} patternLibrary The modified pattern library
*/
addPatterns: function (patterns) {
Expand All @@ -506,10 +522,10 @@
* Take a valid jQuery selector, and a list of valid values to
* validate against.
* If the user input isn't in the list, validation fails.
*
*
* @param {String} selector Any valid jQuery selector.
*
* @param {Array} values A list of valid values to validate selected
* @param {Array} values A list of valid values to validate selected
* fields against.
*/
validValues: function (selector, values) {
Expand Down