-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidator.min.js
8 lines (8 loc) · 3.57 KB
/
validator.min.js
1
2
3
4
5
6
7
8
//
// validator.js
// validationTests
//
// Created by Robbie Bardijn on 2012-06-07.
// Copyright 2012 Robbie Bardijn. All rights reserved.
//
function Validator(){this.hasErrors=!1}Validator.hassErrors=function(){return this.hasErrors};Validator.validate=function(e,t){this.hasErrors=!1;var n;if(t===undefined)for(n in e){if(e[n].validator===undefined||e[n].validator!==!0&&e[n].validator!==!1)throw"you must specify a validator key in a validator object, the value of this key Must be a function";if(e[n].onFail===undefined||e[n].onFail instanceof Function==0)throw"you must specify a onFail key in a validator object, the value of this key Must be a function";if(e[n].validator===!0)e[n].onSuccess!==undefined&&e[n].onSuccess();else{this.hasErrors=!0;e[n].onFail()}}else{if(e[t]===undefined)throw"you must specify a matching singlerule key from your validators object to validate";if(e[t].validator===undefined||e[t].validator!==!0&&e[t].validator!==!1)throw"you must specify a validator key in a validator object, the value of this key Must be a function";if(e[t].onFail===undefined||e[t].onFail instanceof Function==0)throw"you must specify a onFail key in a validator object, the value of this key Must be a function";if(e[t].validator===!0)e[n].onSuccess!==undefined&&e[n].onSuccess();else{this.hasErrors=!0;e[t].onFail()}}};String.prototype.ltrim=function(){return this.replace(/^\s+/,"")};String.prototype.rtrim=function(){return this.replace(/\s+$/,"")};Validator.validateUsernameOrPassword=function(e){var t=/^[a-zA-Z0-9_?\-?]+$/;return e.match(t)?!0:!1};Validator.validateHexvalue=function(e){var t=/^#?([a-f0-9]{6}|[a-f0-9]{3})$/;return e.match(t)?!0:!1};Validator.validateEmail=function(e){var t=/^([a-zA-Z0-9_\.\-]+)@([\da-zA-Z\.\-]+)\.([a-zA-Z\.]{2,6})$/;return e.match(t)?!0:!1};Validator.validateUrl=function(e){var t=/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;return e.match(t)?!0:!1};Validator.validateIpadress=function(e){var t=/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;return e.match(t)?!0:!1};Validator.validateHtmltag=function(e){var t=/^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/;return e.match(t)?!0:!1};Validator.validateRequired=function(e){return e.length!==0?!0:!1};Validator.validateLength=function(e,t,n){if(t===0&&n===0)throw"Validator : When calling validateLength min or max should be different from 0.";if((t>=n||n<=t)&&n!==0)throw"Validator : When calling validateLength min is the minimum and max should be the maximum.";return t!==0&&n!==0?e.length>t&&e.length<n?!0:!1:t===0&&n!==0?e.length<n?!0:!1:t!==0&&n===0?e.length>t?!0:!1:!1};Validator.isInteger=function(e){return parseInt(e,10)?!0:!1};Validator.validateRange=function(e,t,n){t===undefined?t=0:t=t;n===undefined?n=100:n=n;if((t>=n||n<=t)&&n!==0)throw"Validator : When calling validateRange min is the minimum and max should be the maximum.";var r=e;return t!==0&&n!==0?r>t&&r<n?!0:!1:t===0&&n!==0?r<n?!0:!1:t!==0&&n===0?r>t?!0:!1:!0};Validator.validateDate=function(e){var t=/(0[1-9]|[12][0-9]|3[01])[\/\-\.](0[1-9]|1[012])[\/\-\.](19|20)[0-9]{2}/;return e.match(t)?!0:!1};Validator.validateHour=function(e){var t=/^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$/;return e.match(t)?!0:!1};Validator.validateIsequalto=function(e,t,n){n===undefined?n=!1:n=n;if(n===!0){e=e.ltrim();t=t.ltrim();e=e.rtrim();t=t.rtrim()}return e===t?!0:!1};Validator.validateIsNotequalto=function(e,t,n){n===undefined?n=!1:n=n;if(n===!0){e=e.ltrim();t=t.ltrim();e=e.rtrim();t=t.rtrim()}return e===t?!1:!0};Validator.validateRegExp=function(e,t){return e.match(t)?!0:!1};