-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
100 lines (90 loc) · 2.46 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
const par = document.getElementsByClassName('container1');
const form=document.getElementById('form');
const email=document.getElementById('email');
const pwd=document.getElementById('pass');
var i=1;
function failf(elem,err)
{
const errormes = elem.parentElement.querySelector('small');
errormes.innerText= err;
elem.parentElement.className = 'input fail';
}
function successf(elem)
{
elem.parentElement.className = 'input success';
}
function checkmail(){
const regem= /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.{1}[a-zA-Z0-9-]+$/;
const regem2= /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.{1}[a-zA-Z0-9-]+\.{1}[a-zA-Z0-9-]+\.{1}[a-zA-Z0-9-]+$/;
if(regem.test(email.value) || regem2.test(email.value))
{
successf(email);
}
else{
failf(email,"Email must be of the form [email protected]");
}
}
function checkpass(){
const regpwd = /^.{8,}$/;
if(regpwd.test(pwd.value)){
successf(pwd);
}
else {
failf(pwd,"Password must have minimum eight characters");
}
}
form.addEventListener('submit', async (e)=> {
e.preventDefault();
const regem= /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.{1}[a-zA-Z0-9-]+$/;
const regem2= /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.{1}[a-zA-Z0-9-]+\.{1}[a-zA-Z0-9-]+\.{1}[a-zA-Z0-9-]+$/;
if(regem.test(email.value) || regem2.test(email.value))
{
successf(email);
i=1;
}
else{
failf(email,"Email must be of the form [email protected]");
i=0;
}
const regpwd = /^.{8,}$/;
if(regpwd.test(pwd.value)){
successf(pwd);
i=1;
}
else {
failf(pwd,"Password must have minimum eight characters");
i=0;
}
try{
const response = await submit();
display(response);
}
catch(errorrr){
display("FAILURE");
}
})
function display(errr)
{
console.log(errr)
if(errr.ok){
alert("Successfully logged in");
form.reset();
email.parentElement.className = 'input';
pwd.parentElement.className = 'input';}
else
alert("Incorrect email or password");
}
async function submit(){
const fdata = new FormData(form);
const datasent = Object.fromEntries(fdata);
if(i===1)
{
const res = await fetch('https://reqres.in/api/login',{
method: 'POST' ,
headers: {
'Content-Type': 'application/json'
} ,
body: JSON.stringify(datasent)})
return res;
}
}