Skip to content

Instantly share code, notes, and snippets.

@mrjithin
Last active October 27, 2021 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrjithin/156c9b0821c1ffc585b4e37593024b91 to your computer and use it in GitHub Desktop.
Save mrjithin/156c9b0821c1ffc585b4e37593024b91 to your computer and use it in GitHub Desktop.
function validate() {
"use strict";
// Focus Contact2
Array.from(document.querySelectorAll('.input2')).forEach(item => {
item.addEventListener('blur', event => {
if(event.target.value.trim() != "") {
event.target.classList.add('has-val');
}
else {
event.target.classList.remove('has-val');
}
})
})
// Validate
const name = document.querySelector('.validate-input input[name="name"]');
const email = document.querySelector('.validate-input input[name="email"]');
const message = document.querySelector('.validate-input textarea[name="message"]');
document.querySelector('.validate-form').addEventListener('submit' ,event => {
let check = true;
if(name.value.trim() == ''){
showValidate(name);
check=false;
}
if(email.value.trim().match(/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{1,5}|[0-9]{1,3})(\]?)$/) == null) {
showValidate(email);
check=false;
}
if(message.value.trim() == ''){
showValidate(message);
check=false;
}
if(!check) event.preventDefault();
});
Array.from(document.querySelectorAll('.validate-form .input2')).forEach(item => {
item.addEventListener("focus", event => {
hideValidate(item);
});
});
function showValidate(input) {
input.parentNode.classList.add('alert-validate');
}
function hideValidate(input) {
input.parentNode.classList.remove('alert-validate');
}
}
validate();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment