Completed
Push — master ( 35e057...d55c4d )
by Ankit
02:09
created

login_validate.js ➔ validateEmail   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
rs 9.4285
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
11
function showError(key, value)
12
{
13
    key = "#"+key;
14
    var selector = "input"+key;
15
    $(selector).prev("span").remove();
16
    $(key).css(errorInput);
17
    var txt = $("<span></span>").text(value).css(errorText);
18
    $(key).before(txt);
19
}
20
21
function login()
22
{
23
    var re = /^\S+@/;
24
    var val = $("#login").val();
25
    $("input#login").prev("span").remove();
26
    // console.log(val);
27
    if(val === "")
28
    {
29
        valLogin = 1;
30
        showError("login", " *Please enter your email or username");
31
    }
32
    else if(re.test(val))
33
    {
34
        var ret = validateEmail(val);
35
        if(!ret)
36
        {
37
            valLogin = 1;
38
            showError("login", " *Invalid Email");
39
        }
40
        else
41
        {
42
            $("#login").css(removeError);
43
            valLogin = 0;
44
        }
45
    }
46
    else
47
    {
48
        $("#login").css(removeError);
49
        valLogin = 0;
50
    }
51
}
52
53
function passwordLogin()
54
{
55
    var val = $("#passLogin").val();
56
    $("input#passLogin").prev("span").remove();
57
    if(val === "")
58
    {
59
        valLogin = 1;
60
        showError("passLogin", " *Enter Password");
61
    }
62
    else
63
    {
64
        $("#passLogin").css(removeError);
65
        valPass = 0;
66
    }
67
}
68
69
function initLogin()
70
{
71
    login();
72
    passwordLogin();
73
}
74
75
$("#login").blur(function()
76
{
77
    login();
78
});
79
80
81
$("#passLogin").blur(function()
82
{
83
    passwordLogin();
84
});
85
86
87
function validateEmail(val)
88
{
89
    var re = /^\S+@\w+\.\w+$/;
90
    return re.test(val);
91
}
92
93
function loginCheck()
94
{
95
    var login = $("#login").val();
96
    var password = $("#passLogin").val();
97
    initLogin();
98
    // console.log(login);
99
    if(valLogin === 0 && valPass === 0)
100
    {
101
        var q = {
102
            "login" : login,
103
            "password" : password
104
        };
105
106
        q = "q=" + JSON.stringify(q);
107
        // console.log(q);
108
        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...
109
        xmlhttp.onreadystatechange = function()
110
        {
111
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
112
            {
113
                var result = JSON.parse(xmlhttp.responseText);
114
                // console.log(result);
115
                if(result["location"])
116
                {
117
                    location.href = result["location"];
118
                }
119
                $(result).each(function(index, element) {
120
                    showError(element["key"], element["value"]);
121
                });
122
            }
123
        };
124
        xmlhttp.open("POST", "ajax/validate_login.php", true);
125
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
126
        xmlhttp.send(q);
127
    }
128
    else
129
    {
130
        // alert("Enter correct details");
131
        $("#myModal").modal();
132
    }
133
}
134
135