Skip to content

Instantly share code, notes, and snippets.

@jlizanab
Last active April 18, 2019 06:47
Show Gist options
  • Save jlizanab/016c40b43e640003162278e4f4c79d3b to your computer and use it in GitHub Desktop.
Save jlizanab/016c40b43e640003162278e4f4c79d3b to your computer and use it in GitHub Desktop.
</*
<form name="formTest" novalidate>
<input id="rut" name="rut" type="text"
ng-model="rut" placeholder="Ej. 11.222.333-4" autocomplete="off" ng-pattern="regexRut"
ng-change="validateRut(formTest.rut)" onkeypress="return (event.charCode >= 48 && event.charCode <= 57) || event.charCode == 45 || event.charCode == 75 || event.charCode == 107;">
</form>
*/
//in Controller
vm.regexRut ="^([0-9]+-[0-9Kk])$";
vm.validateRut=function(formElement){
var dv = function (T) {
var M = 0, S = 1; for (; T; T = Math.floor(T / 10))
S = (S + T % 10 * (9 - M++ % 6)) % 11; return S ? S - 1 : 'k';
}
var rut = formElement.$viewValue;
if (rut) {
var splittedRut=rut.split('-');
if(splittedRut.length === 2){
if (String(dv(splittedRut[0])).toUpperCase() != (splittedRut[1]).toUpperCase()){
formElement.$setValidity("rut", false);
} else {
formElement.$setValidity("rut", true);
}
}else{
formElement.$setValidity("rut", false);
}
}
}
/* CSS/SASS
input[type="text"]{
&.ng-not-empty.ng-invalid{
border:2px solid red;
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment