Last active
June 6, 2016 22:41
-
-
Save bsepulveda/a3a5e8d4373281ceec3e1b3e5bfda71b to your computer and use it in GitHub Desktop.
Validación de RUT Chileno Bootstrap 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="es"> | |
<head> | |
<!-- Required meta tags always come first --> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | |
<meta http-equiv="x-ua-compatible" content="ie=edge"> | |
</head> | |
<body> | |
<div class="container"> | |
<br><br><br> | |
<div class="row"> | |
<div class="card card-block col-xs-10 col-md-8" style="float: none;display: block;margin-left: auto;margin-right: auto;"> | |
<h4 class="card-title">Validar Rut</h4> | |
<form name="form1" id="form1"> | |
<fieldset class="form-rut form-group"> | |
<div class="form-control-label col-md-4 col-xs-10"> | |
<label class="" for="rut">Rut</label> | |
<input type="text" class="input-rut form-control" name="rut" id="rut" placeholder="14.569.484-1" required> | |
</div> | |
</fieldset> | |
<fieldset class="form-nom form-group"> | |
<div class="col-md-10 col-xs-12"> | |
<label for="formGroupExampleInput2">Nombre</label> | |
<input type="text" class="input-nom form-control" id="nom" name="nom" placeholder="Ingrese su Nombres" required> | |
</div> | |
</fieldset> | |
<fieldset class="form-group"> | |
<div class="col-xs-12"> | |
<button type="submit" class="btn btn-secondary">Submit</button> | |
</div> | |
</fieldset> | |
</form> | |
</div> | |
</div> | |
</div> | |
<!-- Bootstrap CSS --> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" integrity="sha384-y3tfxAZXuh4HwSYylfB+J125MxIs6mR5FOHamPBG064zB+AFeWH94NdvaCBm8qnd" crossorigin="anonymous"> | |
<!-- jQuery first, then Bootstrap JS. --> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> | |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/js/bootstrap.min.js" integrity="sha384-vZ2WRJMwsjRMW/8U7i6PWi6AlO1L79snBrmgiDpgIWJ82z8eA5lenwvxbMV1PAh7" crossorigin="anonymous"></script> | |
<!--<script src="js/validarRUT.js"></script>--> | |
<script src="js/jquery.rut.js"></script> | |
<script type="text/javascript"> | |
$(function() { | |
$("#rut").rut({formatOn: 'keyup', | |
validateOn: 'keyup' | |
}).on('rutInvalido', | |
function(){ | |
rut.setCustomValidity("RUT Inválido"); | |
$(".form-rut").addClass("has-danger") | |
$(".input-rut").addClass("form-control-danger") | |
}).on('rutValido', | |
function(){ | |
$(".form-rut").removeClass("has-danger") | |
$(".form-rut").addClass("has-success") | |
$(".input-rut").removeClass("form-control-danger") | |
$(".input-rut").addClass("form-control-success") | |
rut.setCustomValidity('') | |
}); | |
}); | |
$('#nom').on('input', function() { | |
var value = $(this).val(); | |
if(value.length >= 5){ | |
$(".form-nom").removeClass("has-danger") | |
$(".form-nom").addClass("has-success") | |
$(".input-nom").removeClass("form-control-danger") | |
$(".input-nom").addClass("form-control-success") | |
nom.setCustomValidity('') | |
} | |
if(value.length >= 0 && value.length < 5){ | |
nom.setCustomValidity("El Nombre debe tener mas de 5 caracteres"); | |
$(".form-nom").addClass("has-danger") | |
$(".input-nom").addClass("form-control-danger") | |
} | |
}); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// The above copyright notice and this permission notice shall be included in | |
// all copies or substantial portions of the Software. | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
// THE SOFTWARE. | |
// Para obtener este programa bajo otra licencia, póngase en | |
// contacto con @pablomarambio en Twitter. | |
;(function($){ | |
var defaults = { | |
validateOn: 'blur', | |
formatOn: 'blur', | |
ignoreControlKeys: true | |
}; | |
//private methods | |
function clearFormat(value) { | |
return value.replace(/[\.\-]/g, ""); | |
}; | |
function format(value) { | |
rutAndDv = splitRutAndDv(value); | |
var cRut = rutAndDv[0]; var cDv = rutAndDv[1]; | |
if(!(cRut && cDv)) return cRut || value; | |
var rutF = ""; | |
while(cRut.length > 3) { | |
rutF = "." + cRut.substr(cRut.length - 3) + rutF; | |
cRut = cRut.substring(0, cRut.length - 3); | |
} | |
return cRut + rutF + "-" + cDv; | |
}; | |
function isControlKey(e) { | |
return e.type && e.type.match(/^key(up|down|press)/) && | |
( | |
e.keyCode == 8 || // del | |
e.keyCode == 16 || // shift | |
e.keyCode == 17 || // ctrl | |
e.keyCode == 18 || // alt | |
e.keyCode == 20 || // caps lock | |
e.keyCode == 27 || // esc | |
e.keyCode == 37 || // arrow | |
e.keyCode == 38 || // arrow | |
e.keyCode == 39 || // arrow | |
e.keyCode == 40 || // arrow | |
e.keyCode == 91 // command | |
); | |
}; | |
function isValidRut(rut) { | |
if(typeof(rut) !== 'string') return false; | |
var cRut = clearFormat(rut); | |
if(cRut.length < 2) return false; | |
var cDv = cRut.charAt(cRut.length - 1).toUpperCase(); | |
var nRut = parseInt(cRut.substr(0, cRut.length - 1)); | |
if(nRut === NaN) return false; | |
return computeDv(nRut).toString().toUpperCase() === cDv; | |
}; | |
function computeDv(rut) { | |
var suma = 0; | |
var mul = 2; | |
if(typeof(rut) !== 'number') return; | |
rut = rut.toString(); | |
for(var i=rut.length -1;i >= 0;i--) { | |
suma = suma + rut.charAt(i) * mul; | |
mul = ( mul + 1 ) % 8 || 2; | |
} | |
switch(suma % 11) { | |
case 1 : return 'k'; | |
case 0 : return 0; | |
default : return 11 - (suma % 11); | |
} | |
}; | |
function formatInput($input, e) { | |
$input.val(format($input.val())); | |
}; | |
function validateInput($input, e) { | |
if(isValidRut($input.val())) { | |
$input.trigger('rutValido', splitRutAndDv($input.val())); | |
} else { | |
$input.trigger('rutInvalido'); | |
} | |
}; | |
function splitRutAndDv(rut) { | |
var cValue = clearFormat(rut); | |
if(cValue.length == 0) return [null, null]; | |
if(cValue.length == 1) return [cValue, null]; | |
var cDv = cValue.charAt(cValue.length - 1); | |
var cRut = cValue.substring(0, cValue.length - 1); | |
return [cRut, cDv]; | |
}; | |
// public methods | |
var methods = { | |
init: function(options) { | |
if (this.length > 1) { | |
/* Valida multiples objetos a la vez */ | |
for (var i = 0; i < this.length; i++) { | |
console.log(this[i]); | |
$(this[i]).rut(options); | |
}; | |
} else { | |
var that = this; | |
that.opts = $.extend({}, defaults, options); | |
that.opts.formatOn && that.on(that.opts.formatOn, function(e) { | |
if(that.opts.ignoreControlKeys && isControlKey(e)) return; | |
formatInput(that, e); | |
}); | |
that.opts.validateOn && that.on(that.opts.validateOn, function(e) { | |
validateInput(that, e); | |
}); | |
} | |
return this; | |
} | |
}; | |
$.fn.rut = function(methodOrOptions) { | |
if(methods[methodOrOptions]) { | |
return methods[methodOrOptions].apply(this, Array.prototype.slice.call( arguments, 1 )); | |
} else if ( typeof methodOrOptions === 'object' || ! methodOrOptions ) { | |
return methods.init.apply( this, arguments ); | |
} else { | |
$.error("El método " + methodOrOptions + " no existe en jQuery.rut"); | |
} | |
}; | |
$.formatRut = function(rut) { | |
return format(rut); | |
} | |
$.validateRut = function(rut, fn) { | |
if(isValidRut(rut)) { | |
var rd = splitRutAndDv(rut); | |
$.isFunction(fn) && fn(rd[0], rd[1]); | |
return true; | |
} else { | |
return false; | |
} | |
} | |
})(jQuery); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* jQuery.rut.js */ | |
(function(e){function n(e){return e.replace(/[\.\-]/g,"")}function r(e){rutAndDv=f(e);var t=rutAndDv[0];var n=rutAndDv[1];if(!(t&&n))return t||e;var r="";while(t.length>3){r="."+t.substr(t.length-3)+r;t=t.substring(0,t.length-3)}return t+r+"-"+n}function i(e){return e.type&&e.type.match(/^key(up|down|press)/)&&(e.keyCode==8||e.keyCode==16||e.keyCode==17||e.keyCode==18||e.keyCode==20||e.keyCode==27||e.keyCode==37||e.keyCode==38||e.keyCode==39||e.keyCode==40||e.keyCode==91)}function s(e){if(typeof e!=="string")return false;var t=n(e);if(t.length<2)return false;var r=t.charAt(t.length-1).toUpperCase();var i=parseInt(t.substr(0,t.length-1));if(i===NaN)return false;return o(i).toString().toUpperCase()===r}function o(e){var t=0;var n=2;if(typeof e!=="number")return;e=e.toString();for(var r=e.length-1;r>=0;r--){t=t+e.charAt(r)*n;n=(n+1)%8||2}switch(t%11){case 1:return"k";case 0:return 0;default:return 11-t%11}}function u(e,t){e.val(r(e.val()))}function a(e,t){if(s(e.val())){e.trigger("rutValido",f(e.val()))}else{e.trigger("rutInvalido")}}function f(e){var t=n(e);if(t.length==0)return[null,null];if(t.length==1)return[t,null];var r=t.charAt(t.length-1);var i=t.substring(0,t.length-1);return[i,r]}var t={validateOn:"blur",formatOn:"blur",ignoreControlKeys:true};var l={init:function(n){if(this.length>1){for(var r=0;r<this.length;r++){console.log(this[r]);e(this[r]).rut(n)}}else{var s=this;s.opts=e.extend({},t,n);s.opts.formatOn&&s.on(s.opts.formatOn,function(e){if(s.opts.ignoreControlKeys&&i(e))return;u(s,e)});s.opts.validateOn&&s.on(s.opts.validateOn,function(e){a(s,e)})}return this}};e.fn.rut=function(t){if(l[t]){return l[t].apply(this,Array.prototype.slice.call(arguments,1))}else if(typeof t==="object"||!t){return l.init.apply(this,arguments)}else{e.error("El método "+t+" no existe en jQuery.rut")}};e.formatRut=function(e){return r(e)};e.validateRut=function(t,n){if(s(t)){var r=f(t);e.isFunction(n)&&n(r[0],r[1]);return true}else{return false}}})(jQuery) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment