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

register_validate.js ➔ showError   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
var valName = 1;
2
var valEmail = 1;
3
var valUser = 1;
4
var valMob = 1;
5
var valPassRegister = 1;
6
var errorText = '{"font-size":"12px","color":"#a94442","display":"inline-block","padding": "5px","font-weight": "700"}';
7
errorText = JSON.parse(errorText);
8
var errorInput = '{"outline":"none","border-color":"#a94442"}';
9
errorInput = JSON.parse(errorInput);
10
var removeError = '{"outline":"none","border-color":"#ccc"}';
11
removeError = JSON.parse(removeError);
12
13
function initRegister()
14
{
15
    name();
16
    email();
17
    username();
18
    mob();
19
    passwordRegister();
20
}
21
22
// Name validation
23
24
$("#name").blur(function()
25
{
26
    name();
27
});
28
29
// Email validation
30
31
$("#email").keyup(function() {
32
    email();
33
});
34
35
$("#email").blur(function() {
36
    email();
37
});
38
39
40
// username validation
41
42
$("#username").blur(function() {
43
    username();
44
});
45
46
// Mobile validation
47
48
$("#mob").blur(function() {
49
    mob();
50
});
51
52
//Password validation
53
54
$("#passRegister").blur(function() {
55
    passwordRegister();
56
57
});
58
59
function registerCheck() {
60
    var name = $("#name").val();
61
    var email = $("#email").val();
62
    var username = $("#username").val();
63
    var mob = $("#mob").val();
64
    var password = $("#passRegister").val();
65
66
    initRegister();
67
68
    if(valName === 0 && valEmail === 0 && valUser === 0 && valMob === 0 && valPassRegister === 0)
69
    {
70
        var q = {"name":name,"email":email,"username":username,"mob":mob,"password":password};
71
        q = "q=" + JSON.stringify(q);
72
        // console.log(q);
73
        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...
74
        xmlhttp.onreadystatechange = function()
75
        {
76
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
77
            {
78
                var result = JSON.parse(xmlhttp.responseText);
79
                // console.log(result);
80
                if(result["location"])
81
                {
82
                    location.href = result["location"];
83
                }
84
                $(result).each(function(index, element) {
85
                    showError(element["key"], element["value"])
86
                });
87
            }
88
        };
89
        xmlhttp.open("POST", "ajax/validate_register.php", true);
90
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
91
        xmlhttp.send(q);
92
    }
93
    else
94
    {
95
        // alert("Please Fill correct details");
96
        $("#myModal").modal()
97
    }
98
}
99
100
function showError(key, value)
101
{
102
    key = "#"+key;
103
    var selector = "input"+key;
104
    $(selector).prev("span").remove();
105
    $(key).css(errorInput);
106
    var txt = $("<span></span>").text(value).css(errorText);
107
    $(key).before(txt);
108
}
109
110
function name()
111
{
112
    var name = $("#name").val();
113
    $("#name + span").text("");
114
    if(name === "")
115
    {
116
        valName = 1;
117
        showNameError(" *Please input your name");
118
    }
119
    else
120
    {
121
        $("#name").css(removeError);
122
        valName = 0;
123
    }
124
}
125
126
function email()
127
{
128
    var val = $("#email").val();
129
    var ret = validate_email(val);
130
    $("input#email").prev("span").remove();
131
    if(val === "")
132
    {
133
        valEmail = 1;
134
        showEmailError(" *Enter Your email address");
135
    }
136
    else if(!ret)
137
    {
138
        valEmail = 1;
139
        showEmailError(" *Invalid Email");
140
    }
141
    else
142
    {
143
        $("#email").css(removeError);
144
        valEmail = 0;
145
    }
146
}
147
148
function username()
149
{
150
    var val = $("#username").val();
151
    var re = /^\S+@/;
152
153
    $("input#username").prev("span").remove();
154
    if(val === "")
155
    {
156
        valUser = 1;
157
        showUsernameError(" *Enter Your username");
158
    }
159
    else if(re.test(val))
160
    {
161
        valUser = 1;
162
        showUsernameError(" *Invalid username");
163
    }
164
    else
165
    {
166
        $("#username").css(removeError);
167
        valUser = 0;
168
    }
169
}
170
171
function mob()
172
{
173
    var mob = $("#mob").val();
174
    var re = /^[0-9]{10}$/;
175
    $("input#mob").prev("span").remove();
176
    if(mob === "")
177
    {
178
        valMob = 1;
179
        showMobError(" *Enter your mobile no.");
180
    }
181
    else if(!re.test(mob))
182
    {
183
        valMob = 1;
184
        showMobError(" *Enter 10 digit mobile no.");
185
    }
186
    else
187
    {
188
        $("#mob").css(removeError);
189
        valMob = 0;
190
    }
191
}
192
193
function passwordRegister()
194
{
195
    var pass = $("#passRegister").val();
196
    $("input#passRegister").prev("span").remove();
197
    if(pass === "")
198
    {
199
        valPassRegister = 1;
200
        showPassErrorRegister(" *Enter your password");
201
    }
202
    else
203
    {
204
        $("#passRegister").css(removeError);
205
        valPassRegister = 0;
206
    }
207
}
208
209
function validate_email(val)
210
{
211
    var re = /^\S+@\w+\.\w+$/;
212
    return re.test(val);
213
}