-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
72 lines (66 loc) · 2.16 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
const form = document.getElementById('form');
const username = document.getElementById('username');
const email = document.getElementById('email');
const password1 = document.getElementById('password1');
const password2 = document.getElementById('password2');
const phone = document.getElementById('phone')
// Form eventListener
form.addEventListener('submit', (e)=>{
e.preventDefault();
check_empty([username, email, password1, password2]);
check_length(username, 3, 10);
check_length(password1, 6,25);
check_passwords(password1,password2);
validateEmail(email);
validatePhone(phone);
});
function validatePhone(input) {
const re = /^(\+[0-9]{3})-([0-9]{10})$/
if(re.test(input.value)){
showSuccess(input);
}else{
showError(input, `Invalid Phone`);
}
}
function validateEmail(input) {
const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if(re.test(String(input.value.trim()).toLowerCase())){
showSuccess(input);
}else{
showError(input, `Invalid email`);
}
}
function check_passwords(input1, input2){
if(input1.value !== input2.value){
showError(input2, "Passwords donot match")
}
}
function check_length(input, min, max){
if(input.value.length < min || input.value.length > max){
showError(input, `${input.id} must be between ${min} to ${max} characters`)
}else{
showSuccess(input);
}
}
function check_empty(input){
input.forEach(element => {
if(element.value.trim() === ''){
showError(element, `${element.id} should not be empty`);
}else{
showSuccess(element);
}
});
}
// Error and success functions
function showError(input, message){
// console.log("hellooooo")
const parent = input.parentElement;
parent.className = 'form-handler error';
console.log(parent);
const small = parent.querySelector('small');
small.innerHTML = message;
}
function showSuccess(input){
const parent = input.parentElement;
parent.className= 'form-handler success';
}