My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Validar el CIF</title>

<script type="text/javascript">

function formato(cif) {

var resul = false;
// pasar a mayúsculas
var temp = cif.toUpperCase();

if (!/^[A-Za-z0-9]{9}$/.test(temp)) // Son 9 dígitos?
alert ("Longitud incorrecta, un CIF consta de 9 dígitos");
else if (!/^[ABCDEFGHKLMNPQS]/.test(temp))
// Es una letra de las admitidas ?
alert("El primer dígito es incorrecto, debe ser una letra de las siguientes: A,B,C,D,E,F,G,H,K,L,M,N,P,Q,S");
else
resul = true;

return resul;
}

function ValidaCIF(cif) {
// Sumamos los A y B
a = 0;
b = 0;
var calculo = new Array(0,2,4,6,8,1,3,5,7,9);

for(x=2;x<=6;x+=2) {
a = a + parseInt(cif.substr(x,1));
b = b + calculo[parseInt(cif.substr(x-1,1))];
}
b = b + calculo[parseInt(cif.substr(x-1,1))];

// C como suma de a+b
c=a+b;

// El calculo de D
d = (10 -(c%10));

var codigos = new Array('J','A','B','C','D','E','F','G','H','I','J');
alert("El dígito de control es " + d + " o " + codigos[d]);
}

</script>

</head>
<body>
<h1>Validar el CIF</h1>


<form name="Validar CIF" action="" method="post">
<label for="cif">Número de CIF: </label>
<input type="text" name="cif" id="cif" onChange="formato(this.value)">
<input type="button" value="Calcular Digito Control" onClick="ValidaCIF(getElementById('cif').value);">
</form>


<br><br>
<hr>
Art&iacute;culo disponible en: <a href="http://lineadecodigo.com/javascript/validar-un-cif-en-javascript/">http://lineadecodigo.com/javascript/validar-un-cif-en-javascript/</a><br/>
<a href="http://lineadecodigo.com" title="Linea de Codigo">lineadecodigo.com</a>

</body>
</html>

Change log

r240 by vcuervo on May 25, 2010   Diff
Validar el CIF
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 1917 bytes, 68 lines
Powered by Google Project Hosting