LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » Pure Vanilla JavaScript Custom Form Validation In HTML Forms

Pure Vanilla JavaScript Custom Form Validation In HTML Forms

Pure-Vanilla-JavaScript-Custom-Form-Validation-In-HTML-Forms
This JavaScript code snippet helps you to create a form validation feature on the form submission. It validates username, email, and password and displays the inline error message in case of invalid input. You can integrate this vanilla JavaScript code for registration/signup forms to validate inputs on submission.

This form validation snippet doesn’t require any additional library or plugin. It uses JavaScript regular expressions to validate emails. It allows setting min/max length rules for input validation. Moreover, it can be integrated with your existing HTML forms.

There are many code snippets available online or on many other blogs and websites, but everyone cannot optimize your blog or website, so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.

Features:

  1. Light Weight.
  2. Pure JavaScript.
  3. Cross Browser.
  4. No JQuery Files.
  5. Fully Customizable.
  6. Responsive.

How To Add Pure Vanilla JavaScript Custom Form Validation In HTML Forms?

There are a few easy and understandable steps to achieve your desired functionality that we are gonna share below. Follow each step perfectly.

Recommended For You:
How To Add Link To SWF Or Flash File To Open A href="www" URL?

CSS:

<style type="text/css">
:root{
--succes-color: #2ecc71;;
--error-color: #e74c3c;
}
.container{
background-color: #fff;
border-radius: 5px;
box-shadow: 0 2px 10px rgba(0,0,0,0.3);
width: 400px;
margin: 10px auto;
}

h2{
text-align: center;
margin: 0 0 20px;
}

.form{
padding: 30px 40px;
}

.form-control{
margin-bottom: 10px;
padding-bottom: 20px;
position: relative;
}

.form-control label{
color:#777;
display: block;
margin-bottom: 5px; 
}

.form-control input
{
border: 2px solid #f0f0f0;
border-radius: 4px;
display: block;
width: 100%;
padding: 10px;
font-size: 14px; 
}

.form-control input:focus{
outline: 0;
border-color: #777;

}

.form-control.success input {
border-color: var(--succes-color);
}

.form-control.error input {
border-color: var(--error-color); 
}

.form-control small{
color: var(--error-color);
position: absolute;
bottom: 0;
left: 0;
visibility: hidden;
}

.form-control.error small{
visibility: visible;
}
.form button {
cursor: pointer;
background-color: #3498db;
border: 2px solid #3498db;
border-radius: 4px;
color: #fff;
display: block;
padding: 10px;
font-size: 16px;
margin-top:20px;
width:100%;
}

</style>

HTML:

<form id="form" class="form">
<h2>Register With Us</h2>
<div class="form-control">
<label for="username">Username</label>
<input type="text" id="username" placeholder="Enter Username">
<small>Error Message</small>
</div>
<div class="form-control">
<label for="email">Email</label>
<input type="text" id="email" placeholder="Enter email">
<small>Error Message</small>
</div>
<div class="form-control">
<label for="password">Password</label>
<input type="password" id="password" placeholder="Enter password">
<small>Error Message</small>
</div>
<div class="form-control">
<label for="password2">Confirm Password</label>
<input type="password" id="password2" placeholder="Enter password again">
<small>Error Message</small>
</div>
<button>Submit</button>
</form>

JavaScript:

<script type="text/javascript">
const form = document.getElementById('form');
const username = document.getElementById('username');
const email = document.getElementById('email');
const password = document.getElementById('password');
const password2 = document.getElementById('password2');

//Show input error messages
function showError(input, message) {
const formControl = input.parentElement;
formControl.className = 'form-control error';
const small = formControl.querySelector('small');
small.innerText = message;
}

//show success colour
function showSucces(input) {
const formControl = input.parentElement;
formControl.className = 'form-control success';
}

//check email is valid
function checkEmail(input) {
const re = /^(([^<>()\[\]\\.,;:\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,}))$/;
if(re.test(input.value.trim())) {
showSucces(input)
}else {
showError(input,'Email is not invalid');
}
}


//checkRequired fields
function checkRequired(inputArr) {
inputArr.forEach(function(input){
if(input.value.trim() === ''){
showError(input,`${getFieldName(input)} is required`)
}else {
showSucces(input);
}
});
}


//check input Length
function checkLength(input, min ,max) {
if(input.value.length < min) {
showError(input, `${getFieldName(input)} must be at least ${min} characters`);
}else if(input.value.length > max) {
showError(input, `${getFieldName(input)} must be les than ${max} characters`);
}else {
showSucces(input);
}
}

//get FieldName
function getFieldName(input) {
return input.id.charAt(0).toUpperCase() + input.id.slice(1);
}

// check passwords match
function checkPasswordMatch(input1, input2) {
if(input1.value !== input2.value) {
showError(input2, 'Passwords do not match');
}
}


//Event Listeners
form.addEventListener('submit',function(e) {
e.preventDefault();

checkRequired([username, email, password, password2]);
checkLength(username,3,15);
checkLength(password,6,25);
checkEmail(email);
checkPasswordMatch(password, password2);
}); 

</script>

Customization:

No need to customize it. Just copy-paste. Rest edit the code as per comments and need.

Recommended For You:
JavaScript Confirmation POPUP To Run Next Function Or Go Back

Troubleshooting the Errors:

Do it with concentration and patience. Check your all steps again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave.

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *