-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
74 lines (64 loc) · 2.12 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
// Show input error message
function showError(input, message) {
const formControl = input.parentElement;
formControl.className = 'form-control error';
const small = formControl.querySelector('small');
small.innerText = message;
}
// Show success outline
function showSuccess(input) {
const formControl = input.parentElement;
formControl.className = 'form-control success';
}
// Check required fields
function checkRequired(inputArr) {
let isRequired = false;
inputArr.forEach(function(input) {
let field_value = document.getElementById(input);
if (field_value.value.trim() === '') {
if (input!="lastname"){
document.getElementById(input).placeholder = "";
showError(field_value, `${getFieldName(field_value)} can not be empty`);
isRequired = true;
}
}
else {
if (input=="firstname" || input=="lastname" || input=="password"){
if (field_value.value.length < 3) {
showError(field_value, `${getFieldName(field_value)} can not be less than 3 letters`);
}
else if (field_value.value.length > 13){
showError(field_value, `${getFieldName(field_value)} can not be greater than 13`);
}
else{
let letters = /^[A-Za-z]+$/;
if (input=="firstname" || input=="lastname"){
if (letters.test(field_value.value.trim())) {
showSuccess(field_value);
} else {
showError(field_value, 'Enter only aplhabets');
}
}
}
}
else if (input=="email"){
const re = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
if (re.test(field_value.value.trim())) {
showSuccess(field_value);
} else {
showError(field_value, 'Looks like this is not an email');
}
}
}
});
return isRequired;
}
// Get fieldname
function getFieldName(input) {
return input.id.charAt(0).toUpperCase() + input.id.slice(1);
}
// Event listeners
form.addEventListener('submit', function(e) {
e.preventDefault();
checkRequired(["firstname", "lastname", "email", "password"]) ;
});