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

Last change on this file since 33109 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
RevLine 
[20431]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 */
[20254]9function piwecard_checkField(element, elementType, display, nullIsOK) {
[20109]10        var TEXT = 0;
11        var EMAIL = 1;
12        var NUMBER = 2;
13       
[20254]14        nullIsOK = (typeof nullIsOK === "undefined") ? false : nullIsOK;
15        display = (typeof display === "undefined") ? 'inline' : display;
[20162]16       
17        if (nullIsOK && element.value === '') {
18                return true;
19        } else {
20                switch (elementType) {
21                        case TEXT:
[20202]22                                if (element.value == '' || element.className.indexOf("ecard_defaultTextActive") > 0) {
[20254]23                                        add_error_style(element, display);
[20162]24                                        return false;
25                                } else {
[20254]26                                        remove_error_style(element);
[20109]27                                        return true;
[20162]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)) {
[20254]33                                        add_error_style(element, display);
[20162]34                                        return false;
[20109]35                                } else {
[20254]36                                        remove_error_style(element);
[20162]37                                        return true;
38                                }
39                                break;
40                        case NUMBER:
41                                if (element.value == '') {
[20254]42                                        add_error_style(element, display);
[20109]43                                        return false;
[20162]44                                } else {
45                                        if (parseFloat(element.value) % 1 == 0){
[20254]46                                                remove_error_style(element);
[20162]47                                                return true;
48                                        } else {
[20254]49                                                add_error_style(element, display);
[20162]50                                                return false;
51                                        }
[20109]52                                }
[20162]53                                break;
54                        default:
55                                return false;
56                                break;
57                }
[20109]58        }
[20254]59}
60
[20431]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 */
[20254]66function add_error_style(element, display) {
67        document.getElementById(element.id+'_error').style.display = display;
68        element.className += " ecard_error_input";
69}
70
[20431]71/**
72 * Remove the error style from the field if piwecard_checkField returns true
73 * @param Object element
74 */
[20254]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 , '');
[20109]78}
Note: See TracBrowser for help on using the repository browser.