Completed
Push — master ( 5d5455...35e057 )
by Ankit
02:15
created

login_validate.js ➔ ... ➔ $(result).each   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
var valLogin = 1;
2
var valPass = 1;
3
var errorText = '{"font-size":"12px","color":"#a94442","display":"inline-block","padding": "5px","font-weight": "700"}';
4
errorText = JSON.parse(errorText);
5
var errorInput = '{"outline":"none","border-color":"#a94442"}';
6
errorInput = JSON.parse(errorInput);
7
var removeError = '{"outline":"none","border-color":"#ccc"}';
8
removeError = JSON.parse(removeError);
9
10
function initLogin()
11
{
12
    login();
13
    passwordLogin();
14
}
15
16
$("#login").blur(function()
17
{
18
    login();
19
});
20
21
22
$("#passLogin").blur(function()
23
{
24
    passwordLogin();
25
});
26
27
28
function validate_email(val)
29
{
30
    var re = /^\S+@\w+\.\w+$/;
31
    return re.test(val);
32
}
33
34
function loginCheck()
35
{
36
    var login = $("#login").val();
37
    var password = $("#passLogin").val();
38
    initLogin();
39
    // console.log(login);
40
    if(valLogin === 0 && valPass === 0)
41
    {
42
        var q = {"login":login,"password":password};
43
        q = "q=" + JSON.stringify(q);
44
        // console.log(q);
45
        var xmlhttp = new XMLHttpRequest();
0 ignored issues
show
Bug introduced by
The variable XMLHttpRequest seems to be never declared. If this is a global, consider adding a /** global: XMLHttpRequest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
46
        xmlhttp.onreadystatechange = function()
47
        {
48
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
49
            {
50
                var result = JSON.parse(xmlhttp.responseText);
51
                // console.log(result);
52
                if(result["location"])
53
                {
54
                    location.href = result["location"];
55
                }
56
                $(result).each(function(index, element) {
57
                    showError(element["key"], element["value"])
58
                });
59
            }
60
        };
61
        xmlhttp.open("POST", "ajax/validate_login.php", true);
62
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
63
        xmlhttp.send(q);
64
    }
65
    else
66
    {
67
        // alert("Enter correct details");
68
        $("#myModal").modal()
69
70
    }
71
}
72
73
function showError(key, value)
74
{
75
    key = "#"+key;
76
    var selector = "input"+key;
77
    $(selector).prev("span").remove();
78
    $(key).css(errorInput);
79
    var txt = $("<span></span>").text(value).css(errorText);
80
    $(key).before(txt);
81
}
82
83
function login()
84
{
85
    var re = /^\S+@/;
86
    var val = $("#login").val();
87
    $("input#login").prev("span").remove()
88
    // console.log(val);
89
    if(val === "")
90
    {
91
        valLogin = 1;
92
        showLoginError(" *Please enter your email or username");
93
    }
94
    else if(re.test(val))
95
    {
96
        var ret = validate_email(val);
97
        if(!ret)
98
        {
99
            valLogin = 1;
100
            showLoginError(" *Invalid Email");
101
        }
102
        else
103
        {
104
            $("#login").css(removeError);
105
            valLogin = 0;
106
        }
107
    }
108
    else
109
    {
110
        $("#login").css(removeError);
111
        valLogin = 0;
112
    }
113
}
114
115
function passwordLogin()
116
{
117
    var val = $("#passLogin").val();
118
    $("input#passLogin").prev("span").remove()
119
    if(val === "")
120
    {
121
        valLogin = 1;
122
        showPassErrorLogin(" *Enter Password");
123
    }
124
    else
125
    {
126
        $("#passLogin").css(removeError);
127
        valPass = 0;
128
    }
129
}