├── LICENSE ├── README.md ├── ejemplo └── index.html └── js ├── jquery.rut.chileno.js └── jquery.rut.chileno.min.js /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Kadumedia 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # jQuery Rut Chileno 2 | 3 | Un simple y muy liviano (solo 1.07kb!) plugin para validar y formatear RUT Chilenos. 4 | 5 | Requiere jQuery 1.6 o superior. 6 | 7 | ## Ejemplo rápido 8 | 9 | El modo más simple es agregando la función al input de texto que desees: 10 | 11 | ```JavaScript 12 | $('.input_rut').rut(); 13 | ``` 14 | 15 | ## Opciones 16 | 17 | * formatear : Da formato 12345678-5 en el evento BLUR (default: true) 18 | * on : Evento que ejecuta la verificación (default: 'blur') 19 | * required : Agrega/elimina la opción de hacer el input requerido (default: true) 20 | * placeholder : Agregar/eliminar el texto temporal del input (default: true) 21 | * error_html : Cambia el html cuando un Rut es inválido (default: `'Rut incorrecto'`) 22 | * fn_error(input) : Función ejecutada al encontrar un error (default: mostrar error) 23 | * fn_validado(input) : Función ejecutada al validar el rut correctamente 24 | 25 | 26 | ## Ejemplo cambiando opciones 27 | 28 | ```JavaScript 29 | $('.input_rut').rut({ 30 | fn_error : function(input){ 31 | alert('El rut: ' + input.val() + ' es incorrecto'); 32 | }, 33 | placeholder: false 34 | }); 35 | ``` 36 | 37 | ## Funciones 38 | 39 | También puedes usar las funciones directamente 40 | 41 | * rut.validar(rut) : Retorna TRUE / FALSE dado un rut '12345678-5' 42 | * rut.dv(rut) : Retorna el digito verificador de un rut 43 | * rut.formatear(rut) : Retorna un string con el formato '12345678-5' 44 | * rut.quitar_formato(rut) : Elimina puntos y guion a un rut 45 | 46 | Ejemplo: 47 | 48 | ```JavaScript 49 | 50 | var es_valido = $.rut.validar('12345678-5'); 51 | 52 | if(es_valido){ 53 | alert('rut válido'); 54 | } 55 | 56 | alert($.rut.quitar_formato('12.345.678-5')); 57 | // Produce 123456785 58 | ``` 59 | -------------------------------------------------------------------------------- /ejemplo/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 | 7 | 12 | 22 | 23 | 24 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /js/jquery.rut.chileno.js: -------------------------------------------------------------------------------- 1 | /* 2 | Plugin para validar y formatear un RUT Chileno 3 | Autor: www.kadumedia.com 4 | Mail: hola@kadumedia.com 5 | Versión: 1.7 6 | */ 7 | ;(function($){ 8 | $.fn.rut = function(opt){ 9 | var defaults = $.extend({ 10 | error_html: 'Rut incorrecto', 11 | formatear : true, 12 | on : 'blur', 13 | required : true, 14 | placeholder : true, 15 | fn_error : function(input){ 16 | mostrar_error(input, defaults.error_html); 17 | }, 18 | fn_validado: function(input){} 19 | }, opt); 20 | return this.each(function(){ 21 | var $t = $(this); 22 | $t.wrap(''); 23 | $t.attr('pattern', '[0-9]{1,2}.[0-9]{3}.[0-9]{3}-[0-9Kk]{1}').attr('maxlength', 12); 24 | if(defaults.required) $t.attr('required', 'required'); 25 | if(defaults.placeholder) $t.attr('placeholder', '12.345.678-5'); 26 | if(defaults.formatear){ 27 | $t.on('blur', function(){ 28 | $t.val($.rut.formatear($t.val())); 29 | }); 30 | } 31 | $t.on(defaults.on, function(){ 32 | $('.rut-error').remove(); 33 | if($.rut.validar($t.val()) && $.trim($t.val()) != '') 34 | defaults.fn_validado(); 35 | else 36 | defaults.fn_error($t); 37 | }); 38 | }); 39 | } 40 | function mostrar_error(input, error){ 41 | input.closest('.rut-container').append(error); 42 | } 43 | })(jQuery); 44 | jQuery.rut = { 45 | validar : function(rut){ 46 | if (!/[0-9]{1,2}.[0-9]{3}.[0-9]{3}-[0-9Kk]{1}/.test(rut)) 47 | return false; 48 | var tmp = rut.split('-'); 49 | var dv = tmp[1], rut = tmp[0].split('.').join(''); 50 | if(dv == 'K') dv = 'k'; 51 | return ($.rut.dv(rut) == dv); 52 | }, 53 | dv : function(rut){ 54 | var M=0,S=1; 55 | for(;rut;rut=Math.floor(rut/10)) 56 | S=(S+rut%10*(9-M++%6))%11; 57 | return S ? S-1 : 'k'; 58 | }, 59 | formatear : function(rut){ 60 | var tmp = this.quitar_formato(rut); 61 | var rut = tmp.substring(0, tmp.length - 1), f = ""; 62 | while(rut.length > 3) { 63 | f = '.' + rut.substr(rut.length - 3) + f; 64 | rut = rut.substring(0, rut.length - 3); 65 | } 66 | return ($.trim(rut) == '') ? '' : rut + f + "-" + tmp.charAt(tmp.length-1); 67 | }, 68 | quitar_formato : function(rut){ 69 | rut = rut.split('-').join('').split('.').join(''); 70 | return rut; 71 | } 72 | }; 73 | -------------------------------------------------------------------------------- /js/jquery.rut.chileno.min.js: -------------------------------------------------------------------------------- 1 | !function(a){function b(a,b){a.closest(".rut-container").append(b)}a.fn.rut=function(c){var d=a.extend({error_html:'Rut incorrecto',formatear:!0,on:"blur",required:!0,placeholder:!0,fn_error:function(a){b(a,d.error_html)},fn_validado:function(a){}},c);return this.each(function(){var b=a(this);b.wrap(''),b.attr("pattern","[0-9]{1,2}.[0-9]{3}.[0-9]{3}-[0-9Kk]{1}").attr("maxlength",12),d.required&&b.attr("required","required"),d.placeholder&&b.attr("placeholder","12.345.678-5"),d.formatear&&b.on("blur",function(){b.val(a.rut.formatear(b.val()))}),b.on(d.on,function(){a(".rut-error").remove(),a.rut.validar(b.val())&&""!=a.trim(b.val())?d.fn_validado():d.fn_error(b)})})}}(jQuery),jQuery.rut={validar:function(a){if(!/[0-9]{1,2}.[0-9]{3}.[0-9]{3}-[0-9Kk]{1}/.test(a))return!1;var b=a.split("-"),c=b[1],a=b[0].split(".").join("");return"K"==c&&(c="k"),$.rut.dv(a)==c},dv:function(a){for(var b=0,c=1;a;a=Math.floor(a/10))c=(c+a%10*(9-b++%6))%11;return c?c-1:"k"},formatear:function(a){for(var b=this.quitar_formato(a),a=b.substring(0,b.length-1),c="";a.length>3;)c="."+a.substr(a.length-3)+c,a=a.substring(0,a.length-3);return""==$.trim(a)?"":a+c+"-"+b.charAt(b.length-1)},quitar_formato:function(a){return a=a.split("-").join("").split(".").join("")}}; 2 | --------------------------------------------------------------------------------