summaryrefslogtreecommitdiffstats
path: root/extensions/BMO/web/js/form_validate.js
blob: 53b16ab1c5e40db1a4f740330287a15a3a0b3943 (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This Source Code Form is "Incompatible With Secondary Licenses", as
 * defined by the Mozilla Public License, v. 2.0. */

/**
 * Form Validation and Interaction
 **/

//Makes sure that there is an '@' in the address with a '.'
//somewhere after it (and at least one character in between them)
function isValidEmail(email) {
    var at_index = email.indexOf("@");
    var last_dot = email.lastIndexOf(".");
    return at_index > 0 && last_dot > (at_index + 1);
}

//Takes a DOM element id and makes sure that it is filled out
function isFilledOut(elem_id)  {
    var el = document.getElementById(elem_id);
    if (!el) {
        console.error('Failed to find element: ' + elem_id);
        return false;
    }
    var str = el.value;
    return str.length > 0 && str != "noneselected";
}

function isChecked(elem_id) {
    return document.getElementById(elem_id).checked;
}

function isOneChecked(form_nodelist) {
    for (var i = 0, il = form_nodelist.length; i < il; i++) {
        if (form_nodelist[i].checked)
            return true;
    }
    return false;
}

function fieldValue(elem_id) {
    var el = document.getElementById(elem_id);
    if (!el) {
        console.error('Failed to find element: ' + elem_id);
        return false;
    }
    if (el.type == 'text'
        || el.type == 'textarea'
        || el.type == 'file')
    {
      return el.value;
    }
    return el.options[el.selectedIndex].value;
}