1
How should I call this validation function PIS input when it loses focus?
<script language="javascript" type="text/javascript">
var ftap = "3298765432";
var total = 0;
var i;
var resto = 0;
var numPIS = 0;
var strResto = "";
function ChecaPIS(pis) {
total = 0;
resto = 0;
numPIS = 0;
strResto = "";
numPIS = pis;
if (numPIS == "" || numPIS == null) {
return false;
}
for (i = 0; i <= 9; i++) {
resultado = (numPIS.slice(i, i + 1)) * (ftap.slice(i, i + 1));
total = total + resultado;
}
resto = (total % 11)
if (resto != 0) {
resto = 11 - resto;
}
if (resto == 10 || resto == 11) {
strResto = resto + "";
resto = strResto.slice(1, 2);
}
if (resto != (numPIS.slice(10, 11))) {
return false;
}
return true;
}
function ValidaPis() {
pis = document.formPis.numPIS.value;
if (!ChecaPIS(pis)) {
alert("PIS INVALIDO");
} else {
alert("PIS VALIDO");
}
}
</script>
Marcelo, Thanks for the help, but I forgot to specify. I need the validation to appear in a Span, below the input, and not in an Alert
– Karina Pinheiro
I edited the answer and added the element
span
for the display of the message, this is what you want @Karinapinheiro?– Marcelo de Andrade
Exactly that. Thank you!!
– Karina Pinheiro