-
-
Notifications
You must be signed in to change notification settings - Fork 519
/
Copy pathscript.js
123 lines (109 loc) · 3.1 KB
/
script.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const form= document.getElementById("form");
const uname= document.getElementById("uname");
const email= document.getElementById("email");
const number= document.getElementById("number");
const password= document.getElementById("password");
const cpassword= document.getElementById("cpassword");
//add event
form.addEventListener('submit', (event) =>{
event.preventDefault();
validate();
})
const sendData =(unameVal ,sRate, count) =>{
if(sRate === count){
alert('registration done successfully');
swal("Welcome!" + unameVal, "Registration Successful", "success");
// location.href ='name of page'
}
}
const successMsg = (unameVal) =>{
let formCon = document.getElementsByClassName("form-control");
var count =formCon.length-1;
for(var i = 0; i<formCon.length; i++){
if(formCon[i].className ==='form-control success'){
var sRate = 0 + i;
console.log(sRate);
sendData(unameVal,sRate, count);
}
else{
return false;
}
}
}
const isEmail = (emailVal) => {
var atSymbole = emailVal.indexOf('a');
if(atSymbole < 1)return false;
var dot = emailVal.lastIndexOf('.');
if(dot < atSymbole + 2)return false;
if(dot ===emailVal.length-1) return false;
return true;
}
//define the validation function
const validate = ()=>{
const unameVal= uname.value.trim();
const emailVal= email.value.trim();
const numberVal= number.value.trim();
const passwordVal= password.value.trim();
const cpasswordVal= cpassword.value.trim();
// validate username
if(unameVal === ""){
setErrorMsg(uname, "username can not be blank");
}
else if(unameVal.length<2){
setErrorMsg(uname, "username min 3 char");
}
else{
setSucessMsg(uname);
}
//validate email
if(emailVal === ""){
setErrorMsg(email, "email can not be blank");
}
else if(!isEmail(emailVal)){
setErrorMsg(emailVal, "Not a valid Email");
}
else{
setSucessMsg(email);
}
//validate number
if(numberVal === ""){
setErrorMsg(number, "Number can not be blank");
}
else if(numberVal.length != 10){
setErrorMsg(number, "Not a valid Number");
}
else{
setSucessMsg(number);
}
//validate password
if(passwordVal === ""){
setErrorMsg(password, "Password can not be null");
}
else if(passwordVal.length <= 5){
setErrorMsg(password, "Minimum 6 char required ");
}
else{
setSucessMsg(password);
}
//validate confirm password
if(cpasswordVal === ""){
setErrorMsg(cpassword, "confirm Password can not be null");
}
else if(passwordVal != cpasswordVal){
setErrorMsg(cpassword, "Password not match ");
}
else{
setSucessMsg(cpassword);
}
successMsg(unameVal);
}
function setErrorMsg(input, errormsgs){
const formControl = input.parentElement;
const small = formControl.querySelector('small');
formControl.className = "form-control error";
small.innerText = errormsgs;
}
function setSucessMsg(input ){
const formControl = input.parentElement;
formControl.className = "form-control success";
}