pristinejs
Advanced tools
Comparing version
@@ -185,3 +185,3 @@ (function (global, factory) { | ||
for (var i in fields) { | ||
for (var i = 0; fields[i]; i++) { | ||
var field = fields[i]; | ||
@@ -230,3 +230,3 @@ if (_validateField(field)) { | ||
var valid = true; | ||
for (var i in field.validators) { | ||
for (var i = 0; field.validators[i]; i++) { | ||
var validator = field.validators[i]; | ||
@@ -356,3 +356,3 @@ var params = field.params[validator.name] ? field.params[validator.name] : []; | ||
self.reset = function () { | ||
for (var i in self.fields) { | ||
for (var i = 0; self.fields[i]; i++) { | ||
self.fields[i].errorElements = null; | ||
@@ -359,0 +359,0 @@ } |
@@ -1,1 +0,1 @@ | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(e="undefined"!=typeof globalThis?globalThis:e||self).Pristine=r()}(this,(function(){"use strict";var e={required:"This field is required",email:"This field requires a valid e-mail address",number:"This field requires a number",integer:"This field requires an integer value",url:"This field requires a valid website URL",tel:"This field requires a valid telephone number",maxlength:"This fields length must be < ${1}",minlength:"This fields length must be > ${1}",min:"Minimum value for this field is ${1}",max:"Maximum value for this field is ${1}",pattern:"Please match the requested format"};function r(e){var r=arguments;return this.replace(/\${([^{}]*)}/g,(function(e,t){return r[t]}))}function t(e){return e.pristine.self.form.querySelectorAll('input[name="'+e.getAttribute("name")+'"]:checked').length}var n={classTo:"form-group",errorClass:"has-danger",successClass:"has-success",errorTextParent:"form-group",errorTextTag:"div",errorTextClass:"text-help"},i=["required","min","max","minlength","maxlength","pattern"],s=/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,a={},o=function(r,t){t.name=r,t.msg||(t.msg=e[r]),void 0===t.priority&&(t.priority=1),a[r]=t};function l(e,t,s){var o=this;function l(e,r,t,n){var i=a[t];if(i&&(e.push(i),n)){var s=n.split(",");s.unshift(null),r[t]=s}}function u(e){var t,n=[],i=!0;for(var s in e.validators){var a=e.validators[s],o=e.params[a.name]?e.params[a.name]:[];if(o[0]=e.input.value,!a.fn.apply(e.input,o)){if(i=!1,(t=a.msg)&&t.constructor&&t.call&&t.apply)n.push(a.msg(e.input.value,o));else{var l=e.messages[a.name]||a.msg;n.push(r.apply(l,o))}if(!0===a.halt)break}}return e.errors=n,i}function f(e){if(e.errorElements)return e.errorElements;var r=function(e,r){for(;(e=e.parentElement)&&!e.classList.contains(r););return e}(e.input,o.config.classTo),t=null,n=null;return(t=o.config.classTo===o.config.errorTextParent?r:r.querySelector("."+o.config.errorTextParent))&&((n=t.querySelector(".pristine-error"))||((n=document.createElement(o.config.errorTextTag)).className="pristine-error "+o.config.errorTextClass,t.appendChild(n),n.pristineDisplay=n.style.display)),e.errorElements=[r,n]}function c(e){var r=f(e),t=r[0],n=r[1];t&&(t.classList.remove(o.config.successClass),t.classList.add(o.config.errorClass)),n&&(n.innerHTML=e.errors.join("<br/>"),n.style.display=n.pristineDisplay||"")}function p(e){var r=function(e){var r=f(e),t=r[0],n=r[1];return t&&(t.classList.remove(o.config.errorClass),t.classList.remove(o.config.successClass)),n&&(n.innerHTML="",n.style.display="none"),r}(e)[0];r&&r.classList.add(o.config.successClass)}return function(e,r,t){e.setAttribute("novalidate","true"),o.form=e,o.config=function(e,r){for(var t in r)t in e||(e[t]=r[t]);return e}(r||{},n),o.live=!(!1===t),o.fields=Array.from(e.querySelectorAll("input:not([type^=hidden]):not([type^=submit]), select, textarea")).map(function(e){var r=[],t={},n={};return[].forEach.call(e.attributes,(function(e){if(/^data-pristine-/.test(e.name)){var s=e.name.substr(14);if(s.endsWith("-message"))return void(n[s.slice(0,s.length-8)]=e.value);"type"===s&&(s=e.value),l(r,t,s,e.value)}else~i.indexOf(e.name)?l(r,t,e.name,e.value):"type"===e.name&&l(r,t,e.value)})),r.sort((function(e,r){return r.priority-e.priority})),o.live&&e.addEventListener(~["radio","checkbox"].indexOf(e.getAttribute("type"))?"change":"input",function(e){o.validate(e.target)}.bind(o)),e.pristine={input:e,validators:r,params:t,messages:n,self:o}}.bind(o))}(e,t,s),o.validate=function(e,r){r=e&&!0===r||!0===e;var t=o.fields;!0!==e&&!1!==e&&(e instanceof HTMLElement?t=[e.pristine]:(e instanceof NodeList||e instanceof(window.$||Array)||e instanceof Array)&&(t=Array.from(e).map((function(e){return e.pristine}))));var n=!0;for(var i in t){var s=t[i];u(s)?!r&&p(s):(n=!1,!r&&c(s))}return n},o.getErrors=function(e){if(!e){for(var r=[],t=0;t<o.fields.length;t++){var n=o.fields[t];n.errors.length&&r.push({input:n.input,errors:n.errors})}return r}return e.tagName&&"select"===e.tagName.toLowerCase()?e.pristine.errors:e.length?e[0].pristine.errors:e.pristine.errors},o.addValidator=function(e,r,t,n,i){e instanceof HTMLElement?(e.pristine.validators.push({fn:r,msg:t,priority:n,halt:i}),e.pristine.validators.sort((function(e,r){return r.priority-e.priority}))):console.warn("The parameter elem must be a dom element")},o.addError=function(e,r){(e=e.length?e[0]:e).pristine.errors.push(r),c(e.pristine)},o.reset=function(){for(var e in o.fields)o.fields[e].errorElements=null;Array.from(o.form.querySelectorAll(".pristine-error")).map((function(e){e.parentNode.removeChild(e)})),Array.from(o.form.querySelectorAll("."+o.config.classTo)).map((function(e){e.classList.remove(o.config.successClass),e.classList.remove(o.config.errorClass)}))},o.destroy=function(){o.reset(),o.fields.forEach((function(e){delete e.input.pristine})),o.fields=[]},o.setGlobalConfig=function(e){n=e},o}return o("text",{fn:function(e){return!0},priority:0}),o("required",{fn:function(e){return"radio"===this.type||"checkbox"===this.type?t(this):void 0!==e&&""!==e},priority:99,halt:!0}),o("email",{fn:function(e){return!e||s.test(e)}}),o("number",{fn:function(e){return!e||!isNaN(parseFloat(e))},priority:2}),o("integer",{fn:function(e){return!e||/^\d+$/.test(e)}}),o("minlength",{fn:function(e,r){return!e||e.length>=parseInt(r)}}),o("maxlength",{fn:function(e,r){return!e||e.length<=parseInt(r)}}),o("min",{fn:function(e,r){return!e||("checkbox"===this.type?t(this)>=parseInt(r):parseFloat(e)>=parseFloat(r))}}),o("max",{fn:function(e,r){return!e||("checkbox"===this.type?t(this)<=parseInt(r):parseFloat(e)<=parseFloat(r))}}),o("pattern",{fn:function(e,r){var t=r.match(new RegExp("^/(.*?)/([gimy]*)$"));return!e||new RegExp(t[1],t[2]).test(e)}}),l.addValidator=function(e,r,t,n,i){o(e,{fn:r,msg:t,priority:n,halt:i})},l})); | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(e="undefined"!=typeof globalThis?globalThis:e||self).Pristine=r()}(this,(function(){"use strict";var e={required:"This field is required",email:"This field requires a valid e-mail address",number:"This field requires a number",integer:"This field requires an integer value",url:"This field requires a valid website URL",tel:"This field requires a valid telephone number",maxlength:"This fields length must be < ${1}",minlength:"This fields length must be > ${1}",min:"Minimum value for this field is ${1}",max:"Maximum value for this field is ${1}",pattern:"Please match the requested format"};function r(e){var r=arguments;return this.replace(/\${([^{}]*)}/g,(function(e,t){return r[t]}))}function t(e){return e.pristine.self.form.querySelectorAll('input[name="'+e.getAttribute("name")+'"]:checked').length}var n={classTo:"form-group",errorClass:"has-danger",successClass:"has-success",errorTextParent:"form-group",errorTextTag:"div",errorTextClass:"text-help"},i=["required","min","max","minlength","maxlength","pattern"],s=/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,a={},o=function(r,t){t.name=r,t.msg||(t.msg=e[r]),void 0===t.priority&&(t.priority=1),a[r]=t};function l(e,t,s){var o=this;function l(e,r,t,n){var i=a[t];if(i&&(e.push(i),n)){var s=n.split(",");s.unshift(null),r[t]=s}}function u(e){for(var t,n=[],i=!0,s=0;e.validators[s];s++){var a=e.validators[s],o=e.params[a.name]?e.params[a.name]:[];if(o[0]=e.input.value,!a.fn.apply(e.input,o)){if(i=!1,(t=a.msg)&&t.constructor&&t.call&&t.apply)n.push(a.msg(e.input.value,o));else{var l=e.messages[a.name]||a.msg;n.push(r.apply(l,o))}if(!0===a.halt)break}}return e.errors=n,i}function f(e){if(e.errorElements)return e.errorElements;var r=function(e,r){for(;(e=e.parentElement)&&!e.classList.contains(r););return e}(e.input,o.config.classTo),t=null,n=null;return(t=o.config.classTo===o.config.errorTextParent?r:r.querySelector("."+o.config.errorTextParent))&&((n=t.querySelector(".pristine-error"))||((n=document.createElement(o.config.errorTextTag)).className="pristine-error "+o.config.errorTextClass,t.appendChild(n),n.pristineDisplay=n.style.display)),e.errorElements=[r,n]}function c(e){var r=f(e),t=r[0],n=r[1];t&&(t.classList.remove(o.config.successClass),t.classList.add(o.config.errorClass)),n&&(n.innerHTML=e.errors.join("<br/>"),n.style.display=n.pristineDisplay||"")}function p(e){var r=function(e){var r=f(e),t=r[0],n=r[1];return t&&(t.classList.remove(o.config.errorClass),t.classList.remove(o.config.successClass)),n&&(n.innerHTML="",n.style.display="none"),r}(e)[0];r&&r.classList.add(o.config.successClass)}return function(e,r,t){e.setAttribute("novalidate","true"),o.form=e,o.config=function(e,r){for(var t in r)t in e||(e[t]=r[t]);return e}(r||{},n),o.live=!(!1===t),o.fields=Array.from(e.querySelectorAll("input:not([type^=hidden]):not([type^=submit]), select, textarea")).map(function(e){var r=[],t={},n={};return[].forEach.call(e.attributes,(function(e){if(/^data-pristine-/.test(e.name)){var s=e.name.substr(14);if(s.endsWith("-message"))return void(n[s.slice(0,s.length-8)]=e.value);"type"===s&&(s=e.value),l(r,t,s,e.value)}else~i.indexOf(e.name)?l(r,t,e.name,e.value):"type"===e.name&&l(r,t,e.value)})),r.sort((function(e,r){return r.priority-e.priority})),o.live&&e.addEventListener(~["radio","checkbox"].indexOf(e.getAttribute("type"))?"change":"input",function(e){o.validate(e.target)}.bind(o)),e.pristine={input:e,validators:r,params:t,messages:n,self:o}}.bind(o))}(e,t,s),o.validate=function(e,r){r=e&&!0===r||!0===e;var t=o.fields;!0!==e&&!1!==e&&(e instanceof HTMLElement?t=[e.pristine]:(e instanceof NodeList||e instanceof(window.$||Array)||e instanceof Array)&&(t=Array.from(e).map((function(e){return e.pristine}))));for(var n=!0,i=0;t[i];i++){var s=t[i];u(s)?!r&&p(s):(n=!1,!r&&c(s))}return n},o.getErrors=function(e){if(!e){for(var r=[],t=0;t<o.fields.length;t++){var n=o.fields[t];n.errors.length&&r.push({input:n.input,errors:n.errors})}return r}return e.tagName&&"select"===e.tagName.toLowerCase()?e.pristine.errors:e.length?e[0].pristine.errors:e.pristine.errors},o.addValidator=function(e,r,t,n,i){e instanceof HTMLElement?(e.pristine.validators.push({fn:r,msg:t,priority:n,halt:i}),e.pristine.validators.sort((function(e,r){return r.priority-e.priority}))):console.warn("The parameter elem must be a dom element")},o.addError=function(e,r){(e=e.length?e[0]:e).pristine.errors.push(r),c(e.pristine)},o.reset=function(){for(var e=0;o.fields[e];e++)o.fields[e].errorElements=null;Array.from(o.form.querySelectorAll(".pristine-error")).map((function(e){e.parentNode.removeChild(e)})),Array.from(o.form.querySelectorAll("."+o.config.classTo)).map((function(e){e.classList.remove(o.config.successClass),e.classList.remove(o.config.errorClass)}))},o.destroy=function(){o.reset(),o.fields.forEach((function(e){delete e.input.pristine})),o.fields=[]},o.setGlobalConfig=function(e){n=e},o}return o("text",{fn:function(e){return!0},priority:0}),o("required",{fn:function(e){return"radio"===this.type||"checkbox"===this.type?t(this):void 0!==e&&""!==e},priority:99,halt:!0}),o("email",{fn:function(e){return!e||s.test(e)}}),o("number",{fn:function(e){return!e||!isNaN(parseFloat(e))},priority:2}),o("integer",{fn:function(e){return!e||/^\d+$/.test(e)}}),o("minlength",{fn:function(e,r){return!e||e.length>=parseInt(r)}}),o("maxlength",{fn:function(e,r){return!e||e.length<=parseInt(r)}}),o("min",{fn:function(e,r){return!e||("checkbox"===this.type?t(this)>=parseInt(r):parseFloat(e)>=parseFloat(r))}}),o("max",{fn:function(e,r){return!e||("checkbox"===this.type?t(this)<=parseInt(r):parseFloat(e)<=parseFloat(r))}}),o("pattern",{fn:function(e,r){var t=r.match(new RegExp("^/(.*?)/([gimy]*)$"));return!e||new RegExp(t[1],t[2]).test(e)}}),l.addValidator=function(e,r,t,n,i){o(e,{fn:r,msg:t,priority:n,halt:i})},l})); |
{ | ||
"name": "pristinejs", | ||
"version": "0.1.8", | ||
"version": "0.1.9", | ||
"description": "A tiny vanilla javascript form validation library", | ||
@@ -5,0 +5,0 @@ "main": "dist/pristine.js", |
@@ -118,3 +118,3 @@ import { lang } from './lang'; | ||
for(let i in fields){ | ||
for(let i = 0; fields[i]; i++) { | ||
let field = fields[i]; | ||
@@ -163,3 +163,3 @@ if (_validateField(field)){ | ||
let valid = true; | ||
for(let i in field.validators){ | ||
for(let i = 0; field.validators[i]; i++) { | ||
let validator = field.validators[i]; | ||
@@ -284,3 +284,3 @@ let params = field.params[validator.name] ? field.params[validator.name] : []; | ||
self.reset = function () { | ||
for(let i in self.fields){ | ||
for(let i = 0; self.fields[i]; i++) { | ||
self.fields[i].errorElements = null; | ||
@@ -287,0 +287,0 @@ } |
@@ -5,19 +5,19 @@ import Pristine from "../src/pristine"; | ||
beforeEach(() => { | ||
const fixture = | ||
`<div id="fixture"> | ||
beforeEach(() => { | ||
const fixture = | ||
`<div id="fixture"> | ||
<form id="form" novalidate method="post"> | ||
<div class="form-group"> | ||
<input id="input" type="text" required class="form-control" /> | ||
<textarea id="textarea" required class="form-control" ></textarea> | ||
<select id="select" required class="form-control"> | ||
<option value="">-----</option> | ||
<option value="bangladesh">Bangladesh</option> | ||
<option value="usa">USA</option> | ||
<option value="canada">Canada</option> | ||
</select> | ||
<input id="checkbox" type="checkbox" name="future" required /> | ||
<input id="ch2" type="checkbox" name="future" required /> | ||
<input id="ch3" type="checkbox" name="future" required /> | ||
<input id="input" type="text" required class="form-control" /> | ||
<textarea id="textarea" required class="form-control" ></textarea> | ||
<select id="select" required class="form-control"> | ||
<option value="">-----</option> | ||
<option value="bangladesh">Bangladesh</option> | ||
<option value="usa">USA</option> | ||
<option value="canada">Canada</option> | ||
</select> | ||
<input id="checkbox" type="checkbox" name="future" required /> | ||
<input id="ch2" type="checkbox" name="future" required /> | ||
<input id="ch3" type="checkbox" name="future" required /> | ||
</div> | ||
@@ -27,49 +27,49 @@ </form> | ||
document.body.insertAdjacentHTML('afterbegin', fixture); | ||
document.body.insertAdjacentHTML('afterbegin', fixture); | ||
}); | ||
afterEach(() => { | ||
afterEach(() => { | ||
document.body.removeChild(document.getElementById('fixture')); | ||
}); | ||
for (let id of ["input", "textarea", "select"]){ | ||
for (let id of ["input", "textarea", "select"]){ | ||
it(`should validate required attribute on ${id}`, () => { | ||
it(`should validate required attribute on ${id}`, () => { | ||
let form = document.getElementById("fixture") | ||
let input = document.getElementById(id) | ||
let pristine = new Pristine(form); | ||
let form = document.getElementById("fixture") | ||
let input = document.getElementById(id) | ||
let pristine = new Pristine(form); | ||
expect(pristine.validate(input)).toBe(false); | ||
expect(pristine.validate(input)).toBe(false); | ||
expect(pristine.getErrors(input).length).toBe(1); | ||
expect(pristine.getErrors(input)[0]).toBe("This field is required"); | ||
expect(pristine.getErrors(input).length).toBe(1); | ||
expect(pristine.getErrors(input)[0]).toBe("This field is required"); | ||
input.value = "bangladesh"; | ||
expect(pristine.validate(input)).toBe(true); | ||
input.value = "bangladesh"; | ||
expect(pristine.validate(input)).toBe(true); | ||
}); | ||
} | ||
}); | ||
} | ||
it(`should validate required attribute on checkbox`, () => { | ||
it(`should validate required attribute on checkbox`, () => { | ||
let form = document.getElementById("fixture") | ||
let input = document.getElementById("checkbox") | ||
let pristine = new Pristine(form); | ||
let form = document.getElementById("fixture") | ||
let input = document.getElementById("checkbox") | ||
let pristine = new Pristine(form); | ||
expect(pristine.validate(input)).toBe(false); | ||
expect(pristine.validate(input)).toBe(false); | ||
expect(pristine.getErrors(input).length).toBe(1); | ||
expect(pristine.getErrors(input)[0]).toBe("This field is required"); | ||
expect(pristine.getErrors(input).length).toBe(1); | ||
expect(pristine.getErrors(input)[0]).toBe("This field is required"); | ||
input.checked = true | ||
expect(pristine.validate(input)).toBe(true); | ||
input.checked = true | ||
expect(pristine.validate(input)).toBe(true); | ||
input.checked = false; | ||
expect(pristine.validate(input)).toBe(false); | ||
input.checked = false; | ||
expect(pristine.validate(input)).toBe(false); | ||
document.getElementById("checkbox").checked = true; | ||
expect(pristine.validate(input)).toBe(true); | ||
document.getElementById("checkbox").checked = true; | ||
expect(pristine.validate(input)).toBe(true); | ||
}); | ||
}); | ||
}); | ||
@@ -79,10 +79,10 @@ | ||
beforeEach(() => { | ||
const fixture = | ||
`<div id="fixture"> | ||
beforeEach(() => { | ||
const fixture = | ||
`<div id="fixture"> | ||
<form id="form" novalidate method="post"> | ||
<div class="form-group"> | ||
<input id="min-input" min="10" type="number" class="form-control" /> | ||
<input id="max-input" max="100" type="number" class="form-control" /> | ||
<input id="min-max-input" min="10" max="100" type="number" class="form-control" /> | ||
<input id="min-input" min="10" type="number" class="form-control" /> | ||
<input id="max-input" max="100" type="number" class="form-control" /> | ||
<input id="min-max-input" min="10" max="100" type="number" class="form-control" /> | ||
</div> | ||
@@ -92,6 +92,6 @@ </form> | ||
document.body.insertAdjacentHTML('afterbegin', fixture); | ||
document.body.insertAdjacentHTML('afterbegin', fixture); | ||
}); | ||
afterEach(() => { | ||
afterEach(() => { | ||
document.body.removeChild(document.getElementById('fixture')); | ||
@@ -101,42 +101,42 @@ }); | ||
it('should validate min (max) when empty', () => { | ||
it('should validate min (max) when empty', () => { | ||
let form = document.getElementById("fixture") | ||
let pristine = new Pristine(form); | ||
let form = document.getElementById("fixture") | ||
let pristine = new Pristine(form); | ||
expect(pristine.validate()).toBe(true); | ||
expect(pristine.validate()).toBe(true); | ||
}); | ||
}); | ||
it('should validate min value', () => { | ||
it('should validate min value', () => { | ||
let form = document.getElementById("fixture") | ||
let input = document.getElementById("min-input") | ||
let pristine = new Pristine(form); | ||
let form = document.getElementById("fixture") | ||
let input = document.getElementById("min-input") | ||
let pristine = new Pristine(form); | ||
input.value = 9; | ||
expect(pristine.validate()).toBe(false); | ||
expect(pristine.getErrors()[0].errors.length).toBe(1); | ||
input.value = 9; | ||
expect(pristine.validate()).toBe(false); | ||
expect(pristine.getErrors()[0].errors.length).toBe(1); | ||
input.value = 10; | ||
expect(pristine.validate()).toBe(true); | ||
input.value = 10; | ||
expect(pristine.validate()).toBe(true); | ||
input.value = 11; | ||
expect(pristine.validate()).toBe(true); | ||
input.value = 11; | ||
expect(pristine.validate()).toBe(true); | ||
}); | ||
}); | ||
it('should validate the max value', () => { | ||
it('should validate the max value', () => { | ||
let form = document.getElementById("fixture") | ||
let input = document.getElementById("max-input") | ||
let pristine = new Pristine(form); | ||
let form = document.getElementById("fixture") | ||
let input = document.getElementById("max-input") | ||
let pristine = new Pristine(form); | ||
for (let item of [[99, true], [100, true], [101, false]]) { | ||
input.value = item[0]; | ||
expect(pristine.validate(input, true)).toBe(item[1]); | ||
} | ||
for (let item of [[99, true], [100, true], [101, false]]) { | ||
input.value = item[0]; | ||
expect(pristine.validate(input, true)).toBe(item[1]); | ||
} | ||
}); | ||
}); | ||
@@ -148,11 +148,11 @@ | ||
beforeEach(() => { | ||
const fixture = | ||
`<div id="fixture"> | ||
beforeEach(() => { | ||
const fixture = | ||
`<div id="fixture"> | ||
<form id="form" novalidate method="post"> | ||
<div class="form-group"> | ||
<input id="input-number" type="number" class="form-control" /> | ||
<input id="input-email" type="email" class="form-control" /> | ||
<input id="input-integer" data-pristine-type="integer" class="form-control" /> | ||
<input id="input-pattern" pattern="/^\\d+\\.\\d{2}$/g" class="form-control" /> | ||
<input id="input-number" type="number" class="form-control" /> | ||
<input id="input-email" type="email" class="form-control" /> | ||
<input id="input-integer" data-pristine-type="integer" class="form-control" /> | ||
<input id="input-pattern" pattern="/^\\d+\\.\\d{2}$/g" class="form-control" /> | ||
</div> | ||
@@ -162,71 +162,71 @@ </form> | ||
document.body.insertAdjacentHTML('afterbegin', fixture); | ||
}); | ||
document.body.insertAdjacentHTML('afterbegin', fixture); | ||
}); | ||
afterEach(() => { | ||
document.body.removeChild(document.getElementById('fixture')); | ||
}); | ||
afterEach(() => { | ||
document.body.removeChild(document.getElementById('fixture')); | ||
}); | ||
it('should validate pattern input', () => { | ||
it('should validate pattern input', () => { | ||
let form = document.getElementById("fixture") | ||
let input = document.getElementById("input-pattern") | ||
let pristine = new Pristine(form); | ||
let form = document.getElementById("fixture") | ||
let input = document.getElementById("input-pattern") | ||
let pristine = new Pristine(form); | ||
for (let item of [["22.2", false], ["20", false], ["text", false], ["22.22", true]]) { | ||
input.value = item[0]; | ||
expect(pristine.validate(input, true)).toBe(item[1]); | ||
} | ||
for (let item of [["22.2", false], ["20", false], ["text", false], ["22.22", true]]) { | ||
input.value = item[0]; | ||
expect(pristine.validate(input, true)).toBe(item[1]); | ||
} | ||
}); | ||
}); | ||
it('should validate number input', () => { | ||
it('should validate number input', () => { | ||
let form = document.getElementById("fixture") | ||
let input = document.getElementById("input-number"); | ||
let pristine = new Pristine(form); | ||
let form = document.getElementById("fixture") | ||
let input = document.getElementById("input-number"); | ||
let pristine = new Pristine(form); | ||
// text value does not actually set, because it's a number input. so the following is true | ||
// text value does not actually set, because it's a number input. so the following is true | ||
for (let item of [[20, true], ["20", true], ["text", true]]) { | ||
input.value = item[0]; | ||
expect(pristine.validate(input, true)).toBe(item[1]); | ||
} | ||
for (let item of [[20, true], ["20", true], ["text", true]]) { | ||
input.value = item[0]; | ||
expect(pristine.validate(input, true)).toBe(item[1]); | ||
} | ||
}); | ||
}); | ||
it('should validate integer input', () => { | ||
it('should validate integer input', () => { | ||
let form = document.getElementById("fixture") | ||
let input = document.getElementById("input-integer"); | ||
let pristine = new Pristine(form); | ||
let form = document.getElementById("fixture") | ||
let input = document.getElementById("input-integer"); | ||
let pristine = new Pristine(form); | ||
for (let item of [[20, true], ["20", true], ["20.89", false], [20.89, false], ["text", false]]) { | ||
input.value = item[0]; | ||
expect(pristine.validate(input, true)).toBe(item[1]); | ||
} | ||
for (let item of [[20, true], ["20", true], ["20.89", false], [20.89, false], ["text", false]]) { | ||
input.value = item[0]; | ||
expect(pristine.validate(input, true)).toBe(item[1]); | ||
} | ||
}); | ||
}); | ||
it('should validate email input', () => { | ||
it('should validate email input', () => { | ||
let form = document.getElementById("fixture") | ||
let input = document.getElementById("input-email"); | ||
let pristine = new Pristine(form); | ||
let form = document.getElementById("fixture") | ||
let input = document.getElementById("input-email"); | ||
let pristine = new Pristine(form); | ||
for (let item of [ | ||
["user@exampl.com", true], | ||
["@example.com", false], | ||
["user@example", false], | ||
["a@x.x", false], | ||
["a@x.xl", true], | ||
["a+filter@x.xl", true], | ||
["a b@c.cd", false], | ||
]) { | ||
input.value = item[0]; | ||
expect(pristine.validate(input, true)).toBe(item[1]); | ||
} | ||
}); | ||
for (let item of [ | ||
["user@exampl.com", true], | ||
["@example.com", false], | ||
["user@example", false], | ||
["a@x.x", false], | ||
["a@x.xl", true], | ||
["a+filter@x.xl", true], | ||
["a b@c.cd", false], | ||
]) { | ||
input.value = item[0]; | ||
expect(pristine.validate(input, true)).toBe(item[1]); | ||
} | ||
}); | ||
@@ -237,9 +237,9 @@ }); | ||
beforeEach(() => { | ||
const fixture = | ||
`<div id="fixture"> | ||
beforeEach(() => { | ||
const fixture = | ||
`<div id="fixture"> | ||
<form id="form" novalidate method="post"> | ||
<div class="form-group"> | ||
<input id="min-length-input" minlength="3" type="text" class="form-control" /> | ||
<input id="max-length-input" maxlength="5" type="text" class="form-control" /> | ||
<input id="min-length-input" minlength="3" type="text" class="form-control" /> | ||
<input id="max-length-input" maxlength="5" type="text" class="form-control" /> | ||
</div> | ||
@@ -249,35 +249,35 @@ </form> | ||
document.body.insertAdjacentHTML('afterbegin', fixture); | ||
}); | ||
document.body.insertAdjacentHTML('afterbegin', fixture); | ||
}); | ||
afterEach(() => { | ||
document.body.removeChild(document.getElementById('fixture')); | ||
}); | ||
afterEach(() => { | ||
document.body.removeChild(document.getElementById('fixture')); | ||
}); | ||
it('should validate minlength value', () => { | ||
it('should validate minlength value', () => { | ||
let form = document.getElementById("fixture") | ||
let input = document.getElementById("min-length-input") | ||
let pristine = new Pristine(form); | ||
let form = document.getElementById("fixture") | ||
let input = document.getElementById("min-length-input") | ||
let pristine = new Pristine(form); | ||
for (let item of [["ab", false], ["abc", true], ["4len", true], ["20.89", true]]) { | ||
input.value = item[0]; | ||
expect(pristine.validate(input, true)).toBe(item[1]); | ||
} | ||
for (let item of [["ab", false], ["abc", true], ["4len", true], ["20.89", true]]) { | ||
input.value = item[0]; | ||
expect(pristine.validate(input, true)).toBe(item[1]); | ||
} | ||
}); | ||
}); | ||
it('should validate the maxlength value', () => { | ||
it('should validate the maxlength value', () => { | ||
let form = document.getElementById("fixture") | ||
let input = document.getElementById("max-length-input") | ||
let pristine = new Pristine(form); | ||
let form = document.getElementById("fixture") | ||
let input = document.getElementById("max-length-input") | ||
let pristine = new Pristine(form); | ||
for (let item of [["ab", true], ["12345", true], ["123456", false]]) { | ||
input.value = item[0]; | ||
expect(pristine.validate(input, true)).toBe(item[1]); | ||
} | ||
for (let item of [["ab", true], ["12345", true], ["123456", false]]) { | ||
input.value = item[0]; | ||
expect(pristine.validate(input, true)).toBe(item[1]); | ||
} | ||
}); | ||
}); | ||
@@ -284,0 +284,0 @@ |
@@ -5,8 +5,9 @@ import Pristine from "../src/pristine"; | ||
beforeEach(() => { | ||
const fixture = | ||
`<div id="fixture"> | ||
beforeEach(() => { | ||
const fixture = | ||
`<div id="fixture"> | ||
<form id="form" novalidate method="post"> | ||
<div class="form-group"> | ||
<input id="input" type="text" required class="form-control" /> | ||
<input id="input" type="text" required class="form-control" /> | ||
<input id="hidden-input" type="hidden" required class="form-control" /> | ||
</div> | ||
@@ -16,6 +17,6 @@ </form> | ||
document.body.insertAdjacentHTML('afterbegin', fixture); | ||
document.body.insertAdjacentHTML('afterbegin', fixture); | ||
}); | ||
afterEach(() => { | ||
afterEach(() => { | ||
document.body.removeChild(document.getElementById('fixture')); | ||
@@ -25,28 +26,47 @@ }); | ||
it('should have no errors when validates to true', () => { | ||
it('should have no errors when validates to true', () => { | ||
let form = document.getElementById("fixture") | ||
let input = document.getElementById("input") | ||
let pristine = new Pristine(form); | ||
let form = document.getElementById("fixture") | ||
let input = document.getElementById("input") | ||
let pristine = new Pristine(form); | ||
input.value = "some value"; | ||
input.value = "some value"; | ||
expect(pristine.validate()).toBe(true); | ||
expect(pristine.getErrors().length).toBe(0); | ||
expect(pristine.validate()).toBe(true); | ||
expect(pristine.getErrors().length).toBe(0); | ||
}); | ||
/* | ||
it('should not show error when silently validating', () => { | ||
}); | ||
}); | ||
it('should not validate hidden input', () => { | ||
it('should not proceed when halt is found', () => { | ||
let form = document.getElementById("fixture") | ||
let pristine = new Pristine(form); | ||
}); | ||
expect(pristine.validate()).toBe(false); | ||
expect(pristine.getErrors().length).toBe(1); | ||
it('should clean up when called destroy', () => { | ||
let input = document.getElementById("input") | ||
input.value = "some value"; | ||
expect(pristine.validate()).toBe(true); | ||
expect(pristine.getErrors().length).toBe(0); | ||
}); | ||
*/ | ||
expect(pristine.validate(document.getElementById("input-hidden"))).toBe(true); | ||
}); | ||
/* | ||
it('should not show error when silently validating', () => { | ||
}); | ||
it('should not proceed when halt is found', () => { | ||
}); | ||
it('should clean up when called destroy', () => { | ||
}); | ||
*/ | ||
}); | ||
58015
1.86%1010
1.2%