-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathforgot-script.js
More file actions
70 lines (69 loc) · 2.21 KB
/
forgot-script.js
File metadata and controls
70 lines (69 loc) · 2.21 KB
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
const errBox = (message)=>{
const box = document.querySelector(".error-box");
if(message===""){
box.innerHTML = "";
box.classList.remove("err-show");
}
else{
box.innerHTML = message;
box.classList.add("err-show");
setTimeout(()=>{box.classList.remove("err-show");},3000)
}
}
window.onload = ()=>{
document.querySelector(".exit").addEventListener("click",()=>{
console.log("exit")
window.location.href="./login.html";
})
}
const ValidateForm = (mail,password,name=".")=>
{
if(password!='' && name!='' && mail!=''){
if (/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/.test(mail))
{
return ("");
}
else{
return("Invalid Email");
}
}
return ("All Feilds Required");
}
document.querySelector(".login-form .submit-btn button").addEventListener("click",(e)=>{
e.preventDefault();
let inputs= document.querySelectorAll(".login-form form input");
let data={
"email":inputs[0].value,
"newpass":inputs[1].value
}
console.log(data);
validForm = ValidateForm(data.email,data.newpass)
if(validForm=="")
loginFn(data);
else{
errBox(validForm);
}
});
const loginFn = (data)=>{
var xhr = new XMLHttpRequest();
//xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function() {
if(this.readyState === 4) {
let respData = JSON.parse(this.responseText)
if(xhr.status==200){
console.log(respData);
document.querySelector('.login-form').innerHTML="<h1>Reset Successful!</h1> <a href='./login.html'>login</a>";
document.querySelector('.login-form').classList.add('success');
}
else{
console.log(respData.err);
errBox(respData.err);
}
}
});
xhr.open("POST", "https://shortify-api.herokuapp.com/forgot/password");
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.withCredentials = false;
xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
xhr.send(JSON.stringify(data));
}