-
Notifications
You must be signed in to change notification settings - Fork 0
/
input-validator.html
52 lines (44 loc) · 1.23 KB
/
input-validator.html
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
<!DOCTYPE html>
<html>
<head>
<title>Input validator</title>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
font-size: .8em;
}
</style>
</head>
<body>
<p>Input validator</p>
<input class="js-value-input" placeholder="Enter something"
onkeypress="handleKeydown(event);"
>
<button class="js-button" onclick="
yourValue();
">Submit</button>
<p class="js-your-value"></p>
<script>
function handleKeydown(event) {
if (event.key === 'Enter') {
yourValue();
}
}
// String or number validator
function yourValue() {
const inputElem = document.querySelector('.js-value-input');
let inputValue = String(inputElem.value);
if (inputValue === '') {
document.querySelector('.js-your-value')
.innerHTML = `Empty input`;
} else if (!isNaN(inputValue)) {
document.querySelector('.js-your-value')
.innerHTML = `You entered numbers: ${inputElem.value}`;
} else {
document.querySelector('.js-your-value')
.innerHTML = `You entered a string: ${inputElem.value}`;
}
}
</script>
</body>
</html>