"Form Validation || Ravi Singh"
Bootstrap 4.1.1 Snippet by ravi7284007

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
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<div class="container">
<h1>Sign Up Today!</h1>
<!-- Form -->
<form id="form">
<!-- Full Name -->
<div class="form-group">
<label for="name">Full Name</label>
<input type="text" id="name" placeholder="Full Name" required minlength="4" max="199" name="name">
</div>
<!-- Phone Number -->
<div class="form-group">
<label for="phone">Phone Number</label>
<input type="text" id="phone" placeholder="Phone Number" required pattern="123-456-7890" name="phone">
</div>
<!-- Email Address -->
<div class="form-group">
<label for="email">Email Address</label>
<input type="email" id="email" placeholder="email@address.com" required name="email">
</div>
<!-- Website URL -->
<div class="form-group">
<label for="website">Website URL</label>
<input type="url" id="website" placeholder="https://www.google.com" required name="url">
</div>
<!-- Password -->
<div class="form-group">
<label for="password">Password</label>
<input type="password" id="password" placeholder="Create Password" required
pattern="^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[a-zA-Z]).{8,}$">
</div>
<!-- Confirm Password -->
<div class="form-group">
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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
@import url('https://fonts.googleapis.com/css2?family=Ubuntu:wght@400;500&display=swap');
html {
box-sizing: border-box;
}
body {
margin: 0;
min-height: 100vh;
font-family: 'Ubuntu', sans-serif;
letter-spacing: 2px;
display: flex;
align-items: center;
justify-content: center;
background-color: #f5f4f4;
}
.container {
background: #fff;
padding: 30px;
box-shadow: 0 5px 20px rgba(0, 0, 0, .2);
width: 50vw;
}
.container label {
display: block;
margin-bottom: 5px;
}
.container form input {
width: 100%;
height: 35px;
box-sizing: border-box;
transition: .3s linear;
padding: 10px
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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
const form = document.getElementById('form');
const password1El = document.getElementById('password')
const password2El = document.getElementById('password2')
const messageContainer = document.querySelector('.message-container');
const message = document.getElementById('message');
let isValid = false;
let passwordsMatch = false;
function validateForm() {
// Using constraint API
isValid = form.checkValidity();
console.log(isValid);
if (!isValid) {
message.textContent = 'Please fill out all fields';
message.style.color = 'red';
messageContainer.style.borderColor = 'red'
return;
}
// check to see if passwords match
if (password1El.value === password2El.value) {
passwordsMatch = true;
password1El.style.borderColor = 'green'
password2El.style.borderColor = 'green'
messageContainer.style.borderColor = 'green'
} else {
passwordsMatch = false;
message.textContent = 'Make sure password match.'
messageContainer.style.borderColor = 'red'
password1El.style.borderColor = 'red'
password2El.style.borderColor = 'red'
return;
}
if (isValid && passwordsMatch) {
message.textContent = 'Successfully Registered!'
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Related: See More


Questions / Comments: