After entering the login details that is email and password, the form gets validated but it does not submit to next page? I have used two classes with form-control to give the text box red colour when no character is entered and green colour for success.
<body>
<div class="container">
<div class="header">
<h2>User Login</h2>
</div>
<form class="form" id="form" action="success.html" method="post">
<div class="form-control">
<label>Email</label>
<input type="email" placeholder="[email protected]" id="email">
<i class="fas fa-check-circle"></i>
<i class="fas fa-exclamation-circle"></i>
<small>Error message</small>
</div>
<div class="form-control">
<label>Password</label>
<input type="password" placeholder="Password" id="password">
<i class="fas fa-check-circle"></i>
<i class="fas fa-exclamation-circle"></i>
<small>Error message</small>
</div>
<button type="Submit" id="myBtn">Submit</button>
</form>
</div>
<script type="text/javascript" src="function.js"></script>
</body>
This code snippet below is javascript for my login page
var form = document.getElementById('form');
var email = document.getElementById('email');
var password = document.getElementById('password');
form.addEventListener('submit', (e) => {
e.preventDefault();
checkInputs();
});
function checkInputs() {
var emailValue = email.value.trim();
var passwordValue = password.value.trim();
if (emailValue === '') {
setErrorFor(email, 'Email cannot be blank');
} else if (!isEmail(emailValue)) {
setErrorFor(email, 'Email is not valid');
} else {
setSuccessFor(email);
}
if (passwordValue === '') {
setErrorFor(password, 'Password cannot be blank');
} else {
setSuccessFor(password);
}
return true;
}
function setErrorFor(input, message) {
var formControl = input.parentElement; //.form-control
var small = formControl.querySelector('small');
//add error message inside small
small.innerText = message;
//add error class
formControl.className = 'form-control error';
}
function setSuccessFor(input) {
var formControl = input.parentElement; //.form-control
var small = formControl.querySelector('small');
formControl.className = 'form-control success';
}
function isEmail(email) {
return /^(([^<>()\[\]\\.,;:\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,
})) $ / .test(email);
}