-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.min.js
6 lines (6 loc) · 6.03 KB
/
validate.min.js
1
2
3
4
5
6
/*!
Validate.js
Version : 0.0.1
Author : Bala
Published : 2015-07-27 */
"use strict";var validate=function(){function a(a){function b(){n(a),P(".mycssclass","display:none")}function c(a){return a?d.test(a.trim()):!1}function n(a){try{if(!a)throw"Invalid options";if(!a.validClass)throw"Required validClass";if(!a.invalidClass)throw"Required invalidClass";return Q.setValid(a.validClass),Q.setInvalid(a.invalidClass),a.event||Q.setEvent(a.event),"Success"}catch(b){return"Failed : "+b}}function o(a){return a?e.test(a.trim()):!1}function p(a){return a?f.test(a.trim()):!1}function q(a){if(!a||"object"==typeof a)return!1;if("string"==typeof a&&-1===a.indexOf(","))return p(a);for(var b=Array.isArray(a)?a:a.split(","),c=b.length-1;c>=0;c--)if(!f.test(b[c]))return!1;return!0}function r(a){if(!a||"object"==typeof a)return!1;if("string"==typeof a&&-1===a.indexOf(","))return g.test(a.trim());for(var b=Array.isArray(a)?a:a.split(","),c=b.length-1;c>=0;c--)if(!g.test(a.trim()))return!1;return!0}function s(a){return a?h.test(a.trim()):!1}function t(a){return a?a.trim().length>6&&a.trim().length<20:!1}function u(a){return a?j.test(a.trim()):!1}function v(a){return a?i.test(a.trim()):!1}function w(a){return a?!0:!1}function x(a){return a&&"object"!=typeof a?l.test(a.trim()):!1}function y(a){if(!a||"object"==typeof a)return!1;if("string"==typeof a&&-1===a.indexOf(","))return l.test(a.trim());for(var b=Array.isArray(a)?a:a.split(","),c=b.length-1;c>=0;c--)if(!l.test(a.trim()))return!1;return!0}function z(a){return a?m.test(a.trim()):!1}function A(a){return a?k.test(a.trim()):!1}function B(a){return D(document,a)}function C(a,b){try{if(!a)throw"Form/Div ID required!.";var c=document.getElementById(a);return D(c,b)}catch(d){return O(d),!1}}function D(a,b){try{var c,d=!0;if(a){if(c=E(a),!c.length)throw"Not found validation required input fields";for(var e in c){var f=c[e];if("object"!=typeof f)break;F(f,G(f),b)||(d=!1)}}return d}catch(g){return O(g),!1}}function E(a){if(a){if(a.querySelectorAll)return a.querySelectorAll("[data-type]");var b,c=a.getElementByTagName("input");for(var d in c){if(isNaN(Number(d)))break;c[d].dataset&&c[d].dataset.type&&(b[d]=c[d])}return b}}function F(a,b,c){var d=c||{},e=d.validClass||Q.getValid(),f=d.invalidClass||Q.getInvalid();return a.classList?(a.classList.add(b?e:f),a.classList.remove(b?f:e)):a.className&&(a.className=a.className.replace(b?e:f,""),-1===a.className.indexOf(b?e:f)&&(a.className+=e)),b}function G(a){var b=a.dataset||"";if(b.type&&b.required&&R.hasOwnProperty(b.type))return R[b.type.toLowerCase()](a.value);if(b.type&&R.hasOwnProperty(b.type))return a.value?R[b.type.toLowerCase()](a.value):!0;throw"Invalid Type : "+a.dataset.type+" Name "+a.name}function H(a,b,c){var d=b||{},e=d.event||Q.getEvent();try{var f;if(a){f=E(a);for(var g in f){var h=f[g];if("object"!=typeof h)break;N(h,h.dataset.event?h.dataset.event:e,M,c,b)}return f}}catch(i){O(i)}}function I(a,b){return K(a,b)}function J(a,b){return L(a,b)}function K(a,b){if(a){var c=document.getElementById(a);return L(a),H(c,b,"attach")}return L(document),H(document,b,"attach")}function L(a){if(a){var b=document.getElementById(a);return H(b,"","detach")}return H(document,"","detach")}function M(a){a=a||window.event,F(a.target,G(a.target),"")}function N(a,b,c,d){if(!(a&&b&&c&&d))throw"DOM/Event/Handler/Type required to bind events";if(a.addEventListener)"attach"===d&&a.addEventListener(b,c),"detach"===d&&a.removeEventListener(b,c);else{if(!a.attachEvent)throw"Event Listener is not support in your browswer";"attach"===d&&a.attachEvent(b,c),"detach"===d&&a.detachEvent(b,c)}}function O(a){console.error("Exception : ",a)}function P(a,b){if(document.styleSheets&&0!==document.getElementsByTagName("head").length){var c,d,e,f;if(document.styleSheets.length>0)for(e=0;e<document.styleSheets.length&&(document.styleSheets[e].disabled||(f=document.styleSheets[e].media,d=typeof f,"string"!==d||""!==f&&-1==f.indexOf("screen")?"object"!=d||""!==f.mediaText&&-1==f.mediaText.indexOf("screen")||(c=document.styleSheets[e]):c=document.styleSheets[e],"undefined"==typeof c));e++);if("undefined"==typeof c){var g=document.createElement("style");for(g.type="text/css",document.getElementsByTagName("head")[0].appendChild(g),e=0;e<document.styleSheets.length;e++)document.styleSheets[e].disabled||(c=document.styleSheets[e]);f=c.media,d=typeof f}if("string"==d){for(e=0;e<c.rules.length;e++)if(c.rules[e].selectorText&&c.rules[e].selectorText.toLowerCase()==a.toLowerCase())return void(c.rules[e].style.cssText=b);c.addRule(a,b)}else if("object"==d){var h=c.cssRules?c.cssRules.length:0;for(e=0;h>e;e++)if(c.cssRules[e].selectorText&&c.cssRules[e].selectorText.toLowerCase()==a.toLowerCase())return void(c.cssRules[e].style.cssText=b);c.insertRule(a+"{"+b+"}",h)}}}var Q=function(){var a="valid",b="invalid",c="onblur";return{setValid:function(b){a=b},getValid:function(){return a},setInvalid:function(a){b=a},getInvalid:function(){return b},setEvent:function(a){c=a},getEvent:function(){return c}}}(),R={activate:function(a,b){return I(a,b)},base64:function(a){return c(a)},creditCardNumber:function(a){return o(a)},configure:function(a){return n(a)},deactivate:function(a){return J(a)},email:function(a){return p(a)},emails:function(a){return q(a)},formordiv:function(a,b){return C(a,b)},ipaddress:function(a){return r(a)},name:function(a){return s(a)},numberic:function(a){return v(a)},page:function(a){return B(a)},password:function(a){return t(a)},phone:function(a){return u(a)},phonewithextension:function(a){return A(a)},text:function(a){return w(a)},url:function(a){return x(a)},urls:function(a){return y(a)},zip:function(a){return z(a)}};return b(),R}function b(b){return new a(b)}var c,d=/[^a-zA-Z0-9\/\+=]/i,e=/^\d{8,16}$/,f=/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/,g=/^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})$/i,h=/^[0-9a-zA-Z-_\.\s]+$/,i=/^[0-9]+$/,j=/^[0-9-+]+$/,k=/^[0-9-\s-]+$/,l=/^((http|https):\/\/(\w+:{0,1}\w*@)?(\S+)|)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/,m=/^[0-9a-zA-Z-\s]+$/;return{getInstance:function(a){return c?c.configure(a):c=b(a),c}}}();