source: extensions/Piwecard/js/piwecard.js @ 20431

Last change on this file since 20431 was 20431, checked in by julien1311, 11 years ago

[piwecard] Some code documentation

  • Property svn:eol-style set to LF
File size: 2.1 KB
Line 
1/**
2 * Check if a field is valid
3 * @param Object element to check
4 * @param Integer type of the element (0 is text, 1 is email, 2 is number)
5 * @param String Style of the error displayed (default = inline)
6 * @param Boolean True if the field can be empty, False otherwise
7 * @return True if OK, False otherwise
8 */
9function piwecard_checkField(element, elementType, display, nullIsOK) {
10        var TEXT = 0;
11        var EMAIL = 1;
12        var NUMBER = 2;
13       
14        nullIsOK = (typeof nullIsOK === "undefined") ? false : nullIsOK;
15        display = (typeof display === "undefined") ? 'inline' : display;
16       
17        if (nullIsOK && element.value === '') {
18                return true;
19        } else {
20                switch (elementType) {
21                        case TEXT:
22                                if (element.value == '' || element.className.indexOf("ecard_defaultTextActive") > 0) {
23                                        add_error_style(element, display);
24                                        return false;
25                                } else {
26                                        remove_error_style(element);
27                                        return true;
28                                }
29                                break;
30                        case EMAIL:
31                                var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
32                                if (!filter.test(element.value)) {
33                                        add_error_style(element, display);
34                                        return false;
35                                } else {
36                                        remove_error_style(element);
37                                        return true;
38                                }
39                                break;
40                        case NUMBER:
41                                if (element.value == '') {
42                                        add_error_style(element, display);
43                                        return false;
44                                } else {
45                                        if (parseFloat(element.value) % 1 == 0){
46                                                remove_error_style(element);
47                                                return true;
48                                        } else {
49                                                add_error_style(element, display);
50                                                return false;
51                                        }
52                                }
53                                break;
54                        default:
55                                return false;
56                                break;
57                }
58        }
59}
60
61/**
62 * Style to add to the field if piwecard_checkField returns false
63 * @param Object element
64 * @param String Style of the error displayed (default = inline)
65 */
66function add_error_style(element, display) {
67        document.getElementById(element.id+'_error').style.display = display;
68        element.className += " ecard_error_input";
69}
70
71/**
72 * Remove the error style from the field if piwecard_checkField returns true
73 * @param Object element
74 */
75function remove_error_style(element) {
76        document.getElementById(element.id+'_error').style.display = 'none';
77        element.className = element.className.replace( /(?:^|\s)ecard_error_input(?!\S)/g , '');
78}
Note: See TracBrowser for help on using the repository browser.