1 | /* |
---|
2 | * Copyright (C) 2011 Daniel Garcia <danigm@wadobo.com> |
---|
3 | * |
---|
4 | * This program is free software: you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation, either version 3 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
16 | * |
---|
17 | */ |
---|
18 | |
---|
19 | |
---|
20 | // Validar expresión regular. |
---|
21 | function validate_expr(value, epx) { |
---|
22 | var expr = new RegExp(epx); |
---|
23 | return (value.search(expr) == 0); |
---|
24 | } |
---|
25 | |
---|
26 | // Validar expresión ignorando diferencias entre mayúsculas y minúsculas. |
---|
27 | function validate_expr_nocase(value, epx) { |
---|
28 | var expr = new RegExp(epx, "i"); |
---|
29 | return (value.search(expr) == 0); |
---|
30 | } |
---|
31 | |
---|
32 | function validate_number(value) { |
---|
33 | return validate_expr(value, "^\\d*$"); |
---|
34 | } |
---|
35 | |
---|
36 | function validate_alphanum(value) { |
---|
37 | return validate_expr(value, "^\\w*$"); |
---|
38 | } |
---|
39 | |
---|
40 | function validate_notnull(value) { |
---|
41 | return validate_expr(value, "^.+$") && !validate_expr(value, "^\\s*0\\s*$"); |
---|
42 | } |
---|
43 | |
---|
44 | function validate_number_notnull(value) { |
---|
45 | return validate_number(value) && validate_notnull(value); |
---|
46 | } |
---|
47 | |
---|
48 | function validate_alphanum_notnull(value) { |
---|
49 | return validate_alphanum(value) && validate_notnull(value); |
---|
50 | } |
---|
51 | |
---|
52 | // Validar dirección IPv4. |
---|
53 | function validate_ipadress(value) { |
---|
54 | var octet = '(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])'; |
---|
55 | var regex = '^((?:' + octet + '\\.){3}' + octet + ')?$'; |
---|
56 | return validate_expr(value, regex); |
---|
57 | } |
---|
58 | |
---|
59 | function validate_ipadress_notnull(value) { |
---|
60 | return validate_ipadress(value) && validate_notnull(value); |
---|
61 | } |
---|
62 | |
---|
63 | // Validar direccion MAC (sin contar caracteres ":"). |
---|
64 | function validate_macaddress(value) { |
---|
65 | var regex = '^([0-9a-fA-F]){12}$'; |
---|
66 | return validate_expr(value.replace(/:/g,''), regex); |
---|
67 | } |
---|
68 | |
---|
69 | function validate_macaddress_notnull(value) { |
---|
70 | return validate_macaddress(value) && validate_notnull(value); |
---|
71 | } |
---|
72 | |
---|
73 | // Validar URL. |
---|
74 | function validate_url(value) { |
---|
75 | var octet = '(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])'; |
---|
76 | var regex = '^((ht|f)tps?:\/\/(([a-z0-9]+([\.\-a-z0-9]+)?\.[a-z]{2,5})|((' + octet + '\\.){3}' + octet + '))(:[0-9]{2,5})?(\/.*)?)?$'; |
---|
77 | return validate_expr_nocase(value, regex); |
---|
78 | |
---|
79 | } |
---|
80 | |
---|
81 | function validate_url_notnull(value) { |
---|
82 | return validate_url(value) && validate_notnull(value); |
---|
83 | } |
---|
84 | |
---|
85 | function validate_nameimagefile(value) { |
---|
86 | return validate_expr(value, "^[0-9a-zA-Z]*$"); |
---|
87 | } |
---|
88 | |
---|
89 | // Validar texto: alfanumerico más espacios, subrayado y guiones. |
---|
90 | function validate_text(value) { |
---|
91 | return ! validate_expr(value, /.*['"%&<=>*$+?|\/]/); |
---|
92 | } |
---|
93 | |
---|
94 | function validate_text_notnull(value) { |
---|
95 | return validate_text(value) && validate_notnull(value); |
---|
96 | } |
---|
97 | function validate_notspace(value) { |
---|
98 | return ! validate_expr(value, /.*[ ]/); |
---|
99 | } |
---|
100 | |
---|
101 | function validation_highlight(field) { |
---|
102 | field.focus(); |
---|
103 | field.style.border = "1px solid red"; |
---|
104 | field.style.background = "#fee"; |
---|
105 | } |
---|
106 | |
---|