0% found this document useful (0 votes)
23 views2 pages

(Focus Contact2)

This document contains JavaScript code for form validation. It has the following key functions: 1. Adds and removes a "has-val" class on input fields when they have text or are empty. 2. Validates that name, email, and message fields are not empty on form submit. 3. Checks that the email field has a valid email format. 4. Shows error messages by adding a class to input field parents if validation fails.

Uploaded by

gvarelag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views2 pages

(Focus Contact2)

This document contains JavaScript code for form validation. It has the following key functions: 1. Adds and removes a "has-val" class on input fields when they have text or are empty. 2. Validates that name, email, and message fields are not empty on form submit. 3. Checks that the email field has a valid email format. 4. Shows error messages by adding a class to input field parents if validation fails.

Uploaded by

gvarelag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

(function ($) {

"use strict";

/*==================================================================
[ Focus Contact2 ]*/
$('.input2').each(function(){
$(this).on('blur', function(){
if($(this).val().trim() != "") {
$(this).addClass('has-val');
}
else {
$(this).removeClass('has-val');
}
})
})

/*==================================================================
[ Validate ]*/
var name = $('.validate-input input[name="name"]');
var email = $('.validate-input input[name="email"]');
var message = $('.validate-input textarea[name="message"]');

$('.validate-form').on('submit',function(){
var check = true;

if($(name).val().trim() == ''){
showValidate(name);
check=false;
}

if($(email).val().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).val().trim() == ''){
showValidate(message);
check=false;
}

return check;
});

$('.validate-form .input2').each(function(){
$(this).focus(function(){
hideValidate(this);
});
});
function showValidate(input) {
var thisAlert = $(input).parent();

$(thisAlert).addClass('alert-validate');
}

function hideValidate(input) {
var thisAlert = $(input).parent();

$(thisAlert).removeClass('alert-validate');
}

})(jQuery);

You might also like