-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
67 lines (52 loc) · 1.7 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
var validationForm = document.createElement('FORM');
validationForm.name = 'login';
validationForm.onsubmit = validate;
var age = document.createElement('INPUT');
age.name = 'age';
age.type = 'number';
age.className = 'form-control';
age.placeholder = 'Input your age';
validationForm.appendChild(age);
var username = document.createElement('INPUT');
username.name = 'username';
username.type = 'text';
username.className = 'form-control';
username.placeholder = 'Input your username. e.g. user_myname';
validationForm.appendChild(username);
var date = document.createElement('INPUT');
date.name = 'date';
date.type = 'text';
date.placeholder = 'Input current date: dd/mm/yyyy';
date.className = 'form-control';
validationForm.appendChild(date);
var submit = document.createElement('INPUT');
submit.name = 'submit';
submit.type = 'submit';
submit.className = 'btn btn-success';
submit.value = 'Validate Me';
validationForm.appendChild(submit);
window.onload = function() {
document.body.appendChild(validationForm);
};
function validate(form) {
var username = document.forms['login']["username"].value;
var age = document.forms['login']["age"].value;
var date = document.forms['login']["date"].value;
if (age == "" || date == "" || username == "") {
alert("Please fill in all fields");
return false;
}
if (! /^[0-9]+$/.test(age)) {
alert("Age can contain only numbers");
return false;
}
if (!username.startsWith('user_')) {
alert("Username must start with 'user_'");
return false;
}
if (date !== moment().format('DD/MM/YYYY') && moment()){
alert('Please, input current date in this format: dd/mm/yyyy');
return false;
}
alert("Validation completed successfully");
}