form-request
Version:
The Validation Form Request
1 lines • 3.72 kB
JavaScript
var Validator=function(){function n(){}var r="";return n.prototype.getErrors=function(n){return r.replace("{name}","<strong>"+n+"</strong>")},n.prototype.accepted=function(n){return!(["yes","on",1,"1",!0,"true"].indexOf(n)<=0)||(r="O campo {name} é inválido.",!1)},n.prototype.alpha=function(n){return!!n.match(/^[A-Za-záàâãéèêíïóôõöúçñÁÀÂÃÉÈÍÏÓÔÕÖÚÇÑ ]+$/)||(r="O campo {name} deve ser alpha.",!1)},n.prototype.array=function(n){return!!Array.isArray(n)},n.prototype.between=function(n,r,e){return n>=r&&n<=e},n.prototype.boolean=function(n){return"boolean"==typeof n||["1","0","true","false"].indexOf(n)},n.prototype.confirmed=function(n,r){return n===r},n.prototype.cnpj=function(n){return!!(n=String(n)).match(/([0-9]{2}[\.]?[0-9]{3}[\.]?[0-9]{3}[\/]?[0-9]{4}[-]?[0-9]{2})|([0-9]{3}[\.]?[0-9]{3}[\.]?[0-9]{3}[-]?[0-9]{2})/)},n.prototype.cpf=function(n){return!!(n=String(n)).match(/[/0-9]{3}\.?[0-9]{3}\.?[0-9]{3}\-?[0-9]{2}/)},n.prototype.email=function(n){return!!(n=String(n)).match(/(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?: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]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/)||(r="O campo {name} deve conter um email válido.",!1)},n.prototype.in_array=function(n,e){e=e.split(",");for(var t=0;t<e.length;t++)if(e[t]==n)return!0;return r="O campo {name} deve estar presente na lista: "+e,!1},n.prototype.integer=function(n){return n%1==0&&!isNaN(n%1)||(r="O campo {name} deve ser inteiro.",!1)},n.prototype.max=function(n,e){return n.length<=e||(r="O campo {name} deve ter no máximo "+e+" caracteres.",!1)},n.prototype.min=function(n,e){return n.length>=e||(r="O campo {name} deve ter no mínimo "+e+" caracteres.",!1)},n.prototype.numeric=function(n){return"number"==typeof n||!n.match(/^\d+(\.\d{1,2})?$/)||(r="O campo {name} deve ser do tipo numérico.",!1)},n.prototype.phone=function(n){return!(n=String(n)).match(/^\(?\d{2}\)?[\s-]?[\s9]?\d{4}-?\d{4}$/)||(r="O campo {name} é inválido.",!1)},n.prototype.required=function(n){return n?"undefined"!==(n=String(n))&&""!==n||(r="O campo {name} é obrigário.",!1):(r="O campo {name} é obrigário.",!1)},n.prototype.valid_cpf=function(n){if(!this.cpf(n))return r="O campo {name} é inválido.",!1;if(n=n.replace(/\./g,"").replace("-",""),["11111111111","22222222222","33333333333","44444444444","55555555555","66666666666","77777777777","88888888888","99999999999"].indexOf(n)>0||"00000000000"===n)return r="O campo {name} é inválido.",!1;for(var e,t=0,o=1;o<=9;o++)t+=parseInt(n.substring(o-1,o))*(11-o);if(10!==(e=10*t%11)&&11!==e||(e=0),e!==parseInt(n.substring(9,10)))return r="O campo {name} é inválido.",!1;t=0;for(o=1;o<=10;o++)t+=parseInt(n.substring(o-1,o))*(12-o);return 10!==(e=10*t%11)&&11!==e||(e=0),e===parseInt(n.substring(10,11))||(r="O campo {name} é inválido.",!1)},n.prototype.valid_cnpj=function(n){if(!this.cnpj(n))return r="O campo {name} é inválido.",!1;if("00000000000000"===(n=n.replace(/[^\d]+/g,"")))return r="O campo {name} é inválido.",!1;for(var e=n.length-2,t=n.substring(0,e),o=n.substring(e),a=0,i=e-7,p=e;p>=1;p--)a+=t.charAt(e-p)*i--,i<2&&(i=9);var c=a%11<2?0:11-a%11;if(parseInt(c)!==parseInt(o.charAt(0)))return r="O campo {name} é inválido.",!1;e+=1,t=n.substring(0,e),a=0,i=e-7;for(p=e;p>=1;p--)a+=t.charAt(e-p)*i--,i<2&&(i=9);return c=a%11<2?0:11-a%11,parseInt(c)===parseInt(o.charAt(1))||(r="O campo {name} é inválido.",!1)},n}();