source: admin/WebConsole/jscripts/validators.js @ 5d05b06

Last change on this file since 5d05b06 was 3806a31, checked in by ramon <ramongomez@…>, 7 years ago

#834: Limpiar código JavaScript?: terminar instrucciones con ";" y evitar asignaciones duplicadas.

git-svn-id: https://opengnsys.es/svn/branches/version1.1@5630 a21b9725-9963-47de-94b9-378ad31fedc9

  • Property mode set to 100644
File size: 3.0 KB
Line 
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.
21function 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.
27function validate_expr_nocase(value, epx) {
28        var expr = new RegExp(epx, "i");
29        return (value.search(expr) == 0);
30}
31
32function validate_number(value) {
33        return validate_expr(value, "^\\d*$");
34}
35
36function validate_alphanum(value) {
37        return validate_expr(value, "^\\w*$");
38}
39
40function validate_notnull(value) {
41        return validate_expr(value, "^.+$") && !validate_expr(value, "^\\s*0\\s*$");
42}
43
44function validate_number_notnull(value) {
45        return validate_number(value) && validate_notnull(value);
46}
47
48function validate_alphanum_notnull(value) {
49        return validate_alphanum(value) && validate_notnull(value);
50}
51
52// Validar dirección IPv4.
53function 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
59function validate_ipadress_notnull(value) {
60        return validate_ipadress(value) && validate_notnull(value);
61}
62
63// Validar direccion MAC (sin contar caracteres ":").
64function validate_macaddress(value) {
65        var regex = '^([0-9a-fA-F]){12}$';
66        return validate_expr(value.replace(/:/g,''), regex);
67}
68
69function validate_macaddress_notnull(value) {
70        return validate_macaddress(value) && validate_notnull(value);
71}
72
73// Validar URL.
74function 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
81function validate_url_notnull(value) {
82        return validate_url(value) && validate_notnull(value);
83}
84
85function validate_nameimagefile(value) {
86        return validate_expr(value, "^[0-9a-zA-Z]*$");
87}
88
89// Validar texto: alfanumerico más espacios, subrayado y guiones.
90function validate_text(value) {
91        return ! validate_expr(value, /.*['"%&<=>*$+?|\/]/);
92}
93
94function validate_text_notnull(value) {
95        return validate_text(value) && validate_notnull(value);
96}
97function validate_notspace(value) {
98        return ! validate_expr(value, /.*[ ]/);
99}
100
101function validation_highlight(field) {
102        field.focus();
103        field.style.border = "1px solid red";
104        field.style.background = "#fee";
105}
106
Note: See TracBrowser for help on using the repository browser.