Files
oficial/app/resources/views/layout/body/scripts/rut.blade.php

36 lines
1.2 KiB
PHP
Raw Normal View History

2024-06-18 22:41:03 -04:00
@push('page_scripts')
<script>
class Rut {
static digitoVerificador(rut) {
2024-12-03 16:45:20 -03:00
if (!(typeof rut === 'string' || rut instanceof String)) {
rut = rut.toString()
}
2024-06-18 22:41:03 -04:00
if (rut.length === 0) {
return ''
}
let M = 0, S = 1
for (; rut; rut = Math.floor(rut / 10)) {
S = (S + rut % 10 * (9 - M++ % 6)) % 11
}
return S ? S - 1 : 'K'
}
static format(rut) {
2024-12-03 16:45:20 -03:00
if (!(typeof rut === 'string' || rut instanceof String)) {
rut = rut.toString()
}
2024-06-18 22:41:03 -04:00
if (rut.length === 0) {
return ''
}
2024-07-17 22:33:33 -04:00
rut.replace(/\D/g, '')
2024-06-18 22:41:03 -04:00
return rut.replace(/^(\d{1,2})(\d{3})(\d{3})$/, '$1.$2.$3')
}
static validar(rut, digito) {
2024-12-03 16:45:20 -03:00
if (!(typeof digito === 'string' || digito instanceof String)) {
digito = digito.toString()
}
2025-04-28 18:59:37 -04:00
return Rut.digitoVerificador(rut).toString().toUpperCase() === digito.toUpperCase()
2024-06-18 22:41:03 -04:00
}
}
</script>
@endpush