<!DOCTYPE html>
<html>
<head>
<script data-require="jquery@2.0.3" data-semver="2.0.3" src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet" data-semver="3.3.1" data-require="bootstrap-css@3.3.1" />
<script src="jquery.rut.js"></script>
<script src="script.js"></script>
</head>
<body>
<div class="form-group">
<label for="txtRut">Tu Rut Chileno:</label>
<input type="text" class="form-control" id="txtRut" placeholder="Rut">
</div>
</body>
</html>
$(document).ready(function () {
$('#txtRut').Rut({
rutDelimiter: ".",
enableValidation: true,
onValidationSuccess: function(){
console.log("Success!");
},
onValidationFailure: function(){
console.log("Failure");
}
});
});
This plugin created by Marcelino Jorge Romero Karavia and it is provided with the following license.
LICENSE
-------
The MIT License (MIT)
Copyright (c) [year] [fullname]
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.
;
(function($, window, document, undefined) {
$.fn.Rut = function(options) {
var settings = $.extend({
enableFormatRut: true,
enableFormatDigitoVerificador: true,
rutDelimiter: ".",
digitoVerificadorDelimiter: "-",
enableValidation: false,
onValidationFailure: function() {},
onValidationSuccess: function() {},
triggerFormatOn: 'change'
}, options);
return this.each(function() {
var $this = $(this);
var isRutFormatEnabled = isBoolean(settings.enableFormatRut) && settings.enableFormatRut;
var isFormatDigitoVerificadorEnabled = isBoolean(settings.enableFormatDigitoVerificador) && settings.enableFormatDigitoVerificador;
if (isRutFormatEnabled || isFormatDigitoVerificadorEnabled) {
$this.on(settings.triggerFormatOn, function() {
$this.val(RutApp.FormatRut($this.val(), isRutFormatEnabled, isFormatDigitoVerificadorEnabled, settings.rutDelimiter, settings.digitoVerificadorDelimiter));
});
}
if (isBoolean(settings.enableValidation) && settings.enableValidation) {
$this.on('blur', function() {
ifThisIsFunctionCallIt(
(RutApp.IsRutValid($this.val()) ? settings.onValidationSuccess : settings.onValidationFailure),
this);
});
}
});
};
function isBoolean(objToCheck) {
return (typeof objToCheck === 'boolean');
}
function ifThisIsFunctionCallIt(objToCall, thisParam) {
if ($.isFunction(objToCall)) {
objToCall.call(thisParam);
}
}
var RutApp = (function() {
function StringBuilder(str) {
this.value = str;
return this;
}
StringBuilder.prototype.append = function(str) {
this.value = this.value + str;
return this;
};
StringBuilder.prototype.toString = function() {
return this.value;
};
StringBuilder.prototype.charAt = String.prototype.charAt;
StringBuilder.prototype.insert = function(position, str) {
this.value = this.value.slice(0, position) + str + this.value.slice(position);
return this;
};
StringBuilder.prototype.reverse = function() {
var o = '';
for (var i = this.value.length - 1; i >= 0; i--) o += this.value[i];
return o;
};
String.prototype.sum = function(callback) {
var sum = 0;
for (var i = 0; i < this.length; i++) {
sum += callback(this[i]);
}
return sum;
};
//StringBuilder.prototype.trim = function () {
// this.value.replace(/^\s+|\s+$/gm, "");
// return this;
//};
var digitoVerificadorLetraK = "K",
factorInicial = 2,
factorUltimo = 7,
minimumRutLength = 3;
function calculateDigitoVerificador(sRutSinDigitoVerificador) {
var indiceFactor = factorInicial - 1;
var rutReverted = new StringBuilder(removeFormat(sRutSinDigitoVerificador)).reverse();
var calculatedSum = rutReverted.sum(function(numChr) {
indiceFactor = (indiceFactor == factorUltimo ? factorInicial : ++indiceFactor);
return indiceFactor * parseInt(numChr);
});
var digitoVerificador;
var result = calculatedSum % 11;
switch (result) {
case 0:
digitoVerificador = "0";
break;
case 1:
digitoVerificador = digitoVerificadorLetraK;
break;
default:
digitoVerificador = (11 - result).toString();
break;
}
return digitoVerificador;
}
function formatRut(rutCompleto, bFormatRut, bFormatDigitoVerificador, cFormatRutDelimiter, cFormatDigitoVerificadorDelimiter) {
var rutPurificado = removeFormat(rutCompleto);
if (rutPurificado.length < minimumRutLength) return rutPurificado;
var formatRutWithDelimiter = function(rutSinDigitoVerificador, strToFormat) {
while (rutSinDigitoVerificador.length > minimumRutLength) {
strToFormat.insert(0, cFormatRutDelimiter + rutSinDigitoVerificador.substring(rutSinDigitoVerificador.length - minimumRutLength));
rutSinDigitoVerificador = rutSinDigitoVerificador.substring(0, rutSinDigitoVerificador.length - minimumRutLength);
}
strToFormat.insert(0, rutSinDigitoVerificador);
return strToFormat;
};
var digitoVerificador = rutPurificado.charAt(rutPurificado.length - 1);
var rutSinDv = rutPurificado.substring(0, rutPurificado.length - 1);
var formatted = (bFormatRut) ? formatRutWithDelimiter(rutSinDv, new StringBuilder('')) : new StringBuilder(rutSinDv);
if (bFormatDigitoVerificador) formatted.append(cFormatDigitoVerificadorDelimiter);
return formatted.append(digitoVerificador).toString();
}
function getRutSinDigitoVerificador(sRutCompleto) {
var rutPurificado = removeFormat(sRutCompleto);
return rutPurificado.substring(0, rutPurificado.length - 1);
}
function getDigitoVerificador(sRutCompleto) {
return sRutCompleto.charAt(sRutCompleto.length - 1);
}
function removeFormat(sRutCompleto) {
var sRut = sRutCompleto;
return sRut.replace(/^0+|[^0-9|k|K]/g, '');
}
function isDigitoVerificadorValidChar(sDv) {
return /^[\d|k|K]$/.test(sDv);
}
function isRutValid(sRutCompleto) {
var sDv = getDigitoVerificador(sRutCompleto);
if (!isDigitoVerificadorValidChar(sDv)) return false;
var rutSinDv = removeFormat(getRutSinDigitoVerificador(sRutCompleto));
return calculateDigitoVerificador(rutSinDv) == sDv;
}
return {
FormatRut: formatRut,
RemoveFormat: removeFormat,
IsDigitoVerificadorValidChar: isDigitoVerificadorValidChar,
CalculateDigitoVerificador: calculateDigitoVerificador,
IsRutValid: isRutValid,
GetRutSinDigitoVerificador: getRutSinDigitoVerificador,
GetDigitoVerificador: getDigitoVerificador
};
})(window);
})(jQuery);
!function(a,b,c,d){function e(a){return"boolean"==typeof a}function f(b,c){a.isFunction(b)&&b.call(c)}a.fn.Rut=function(b){var c=a.extend({enableFormatRut:!0,enableFormatDigitoVerificador:!0,rutDelimiter:".",digitoVerificadorDelimiter:"-",enableValidation:!1,onValidationFailure:function(){},onValidationSuccess:function(){},triggerFormatOn:"change"},b);return this.each(function(){var b=a(this),d=e(c.enableFormatRut)&&c.enableFormatRut,h=e(c.enableFormatDigitoVerificador)&&c.enableFormatDigitoVerificador;(d||h)&&b.on(c.triggerFormatOn,function(){b.val(g.FormatRut(b.val(),d,h,c.rutDelimiter,c.digitoVerificadorDelimiter))}),e(c.enableValidation)&&c.enableValidation&&b.on("blur",function(){f(g.IsRutValid(b.val())?c.onValidationSuccess:c.onValidationFailure,this)})})};var g=function(){function a(a){return this.value=a,this}function f(e){var i,f=c-1,g=new a(j(e)).reverse(),h=g.sum(function(a){return f=f==d?c:++f,f*parseInt(a)}),k=h%11;switch(k){case 0:i="0";break;case 1:i=b;break;default:i=(11-k).toString()}return i}function g(b,c,d,f,g){var h=j(b);if(h.length<e)return h;var i=function(a,b){for(;a.length>e;)b.insert(0,f+a.substring(a.length-e)),a=a.substring(0,a.length-e);return b.insert(0,a),b},k=h.charAt(h.length-1),l=h.substring(0,h.length-1),m=c?i(l,new a("")):new a(l);return d&&m.append(g),m.append(k).toString()}function h(a){var b=j(a);return b.substring(0,b.length-1)}function i(a){return a.charAt(a.length-1)}function j(a){var b=a;return b.replace(/^0+|[^0-9|k|K]/g,"")}function k(a){return/^[\d|k|K]$/.test(a)}function l(a){var b=i(a);if(!k(b))return!1;var c=j(h(a));return f(c)==b}a.prototype.append=function(a){return this.value=this.value+a,this},a.prototype.toString=function(){return this.value},a.prototype.charAt=String.prototype.charAt,a.prototype.insert=function(a,b){return this.value=this.value.slice(0,a)+b+this.value.slice(a),this},a.prototype.reverse=function(){for(var a="",b=this.value.length-1;b>=0;b--)a+=this.value[b];return a},String.prototype.sum=function(a){for(var b=0,c=0;c<this.length;c++)b+=a(this[c]);return b};var b="K",c=2,d=7,e=3;return{FormatRut:g,RemoveFormat:j,IsDigitoVerificadorValidChar:k,CalculateDigitoVerificador:f,IsRutValid:l,GetRutSinDigitoVerificador:h,GetDigitoVerificador:i}}(b)}(jQuery);