HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<html>
<head>
<meta charset="UTF-8">
<link href="https://fonts.googleapis.com/css?family=Open+Sans:100,200,300,400,500,600,700,800,900" rel="stylesheet">
</head>
<body>
<section class="contact-us" id="contact-section">
<form id="contact" action="" method="post">
<div class="section-heading">
<h4>Contact us</h4>
</div>
<div class="inputField">
<input type="name" name="name" id="name" placeholder="Your name" autocomplete="on" required>
<span class="valid_info_name"></span>
</div>
<div class="inputField">
<input type="Email" name="email" id="email" placeholder="Your email" required="" />
<span class="valid_info_email"></span>
</div>
<div class="inputField">
<textarea name="message" id="message" placeholder="Your message"></textarea>
<span class="valid_info_message"></span>
</div>
<div class="inputField btn">
<button type="submit" id="form-submit" class="main-gradient-button" disabled>Send a message</button>
</div>
</form>
</section>
<script src="https://code.jquery.com/jquery-3.6.3.min.js"></script>
</body>
</html>
@import url("https://fonts.googleapis.com/css?family=Open+Sans:100,200,300,400,500,600,700,800,900");
html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, div
pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, font, img, ins, kbd, q,
s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li,
figure, header, nav, section, article, aside, footer, figcaption, button {
margin: 0;
padding: 0;
border: 0;
outline: 0;
}
.html, body {
font-family: 'Open Sans', sans-serif;
background-color: #474a56;
}
section {
margin-top: 50px;
margin-bottom: 50px;
}
section.contact-us #contact {
position: relative;
display: block;
width: 380px;
height: auto;
background-color: #fff;
padding: 40px;
margin-left: auto;
margin-right: auto;
border-radius: 15px;
box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.05);
}
section.contact-us .section-heading {
position: relative;
display: block;
margin: auto;
}
.section-heading h4 {
line-height: 40px;
font-size: 28px;
font-weight: 900;
color: #dc8cdb;
text-align: center;
text-transform: uppercase;
margin-bottom: 40px;
}
input, textarea {
width: 350px;
position: relative;
display: block;
background-color: #f4f7fb;
font-family: 'Open Sans', sans-serif;
font-size: 12px;
font-weight: 500;
border: none;
box-shadow: none;
border-radius: 5px;
outline-color: #9e9e9e;
}
input {
height: 40px;
padding: 0px 15px;
}
textarea {
min-height: 140px;
max-height: 180px;
padding: 15px;
resize: none;
}
.contact-us span {
height: 20px;
font-size: 12px;
margin-bottom: 20px;
}
.valid_info_name, .valid_info_email, .valid_info_message{
display: inline-block;
font-size: 13px;
margin: 5px 2px;
}
.valid {
border: 2px solid green;
outline-color: green;
}
.invalid {
border: 2px solid red;
outline-color: red;
}
.btn {
display: inline-flex;
width: 100%;
justify-content: flex-end;
}
#form-submit {
position: relative;
display: inline-block;
float: right;
font-size: 12px;
font-weight: bold;
letter-spacing: 1px;
text-transform: uppercase;
color: #fff;
background: rgb(219,138,222);
background: linear-gradient(-145deg, rgba(219,138,222,1) 0%, rgba(246,191,159,1) 100%);
padding: 12px 20px;
border-radius: 5px;
border: none;
outline: none;
cursor: pointer;
transition: all .3s;
transition: all .3s;
}
#form-submit:disabled {
border: 1px solid #9e9e9e;
background: transparent;
color: #9e9e9e;
transition: none;
transform: none;
cursor: default;
}
#form-submit:hover:disabled{
border: 1px solid #9e9e9e;
color: #9e9e9e;
background: transparent;
transition: none;
transform: none;
cursor: default;
}
button:active {
transform: scale(0.95);
}
button:focus {
outline: none;
}
button.ghost {
background-color: transparent;
border-color: #FFFFFF;
}
// ---------------------------contact-us validation------------------------
// clear form onload page
$(window).on('pageshow', function() {
$('#contact')[0].reset();
});
$(document).ready(function() {
// keypress enter - false
$('#name, #email, #message').keypress(function (e) {
let key = e.which;
if(key == 13) {
return false;
}
});
//-------------------name validation-------------------
// validate name
$('#name').on('input blur keydown keyup change', function(){
validateName();
});
let nameVal = false;
// validation function
function validateName() {
let name = document.querySelector('#name');
let regex = /^[a-zA-Zа-яА-ЯёЁіІїЇєЄ0-9.\-_ ]+$/;
let minLengthName = 2;
let maxLengthName = 30;
let regexName = /(.)\1{3}/;
let firstChar = $("#name").val().charAt(0);
// check max len
$('#name').on('keydown keypress', function(e) {
let val = $(this).val();
if (val.length >= maxLengthName && e.keyCode !== 8 && e.keyCode !== 46) {
e.preventDefault();
}
});
// checking for an empty string
if (name.value.length == 0) {
$('#name').removeClass('valid');
$('#name').removeClass('invalid');
$('.valid_info_name').text('');
nameVal = false;
return false;
}
// check first character
else if (!(/[a-zA-Zа-яА-ЯёЁіІїЇєЄ]/).test(firstChar)) {
$('#name').removeClass('valid');
$('#name').removeClass('invalid');
$('.valid_info_name').text("The name entered is invalid. The first character must be a letter").css('color', 'red');
$('#name').addClass('invalid');
nameVal = false;
return false;
}
// length check
else if ((0 < name.value.length && name.value.length < minLengthName) || (name.value.length > maxLengthName)) {
$('#name').removeClass('valid');
$('#name').removeClass('invalid');
$('.valid_info_name').text("The name entered is invalid. The name must be between " + minLengthName + " and " + maxLengthName + " characters").css('color', 'red');
$('#name').addClass('invalid');
nameVal = false;
return false;
}
// input validation no more than 3 identical characters in a row
else if (regexName.test($('#name').val())) {
$('#name').removeClass('valid');
$('#name').removeClass('invalid');
$('.valid_info_name').text("The name entered is invalid. Too many identical characters").css('color', 'red');
$('#name').addClass('invalid');
nameVal = false;
return false;
}
// checking for valid characters
else if (!regex.test($('#name').val())) {
$('#name').removeClass('valid');
$('#name').removeClass('invalid');
$('.valid_info_name').text("The name entered is invalid. The name can only contain letters, numbers, spaces and symbols (._-)").css('color', 'red');
$('#name').addClass('invalid');
nameVal = false;
return false;
}
else {
$('#name').removeClass('valid');
$('#name').removeClass('invalid');
$('.valid_info_name').text("The entered name is valid").css('color', 'green');
$('#name').addClass('valid');
// check first character
if (!(/[a-zA-Zа-яА-ЯёЁіІїЇєЄ]/).test(firstChar)) {
$('#name').removeClass('valid');
$('#name').removeClass('invalid');
$('.valid_info_name').text("The name entered invalid. The first character must be a letter").css('color', 'red');
$('#name').addClass('invalid');
nameVal = false;
return false;
}
nameVal = true;
return true;
}
}
// prohibit entering more than 3 identical characters in a row
$(document).ready(function() {
$('#name').on('keypress', function(e) {
let re = /(.)\1{2}/;
if (re.test($('#name').val())) {
e.preventDefault();
}
});
});
// first character can only be [a-zA-Zа-яА-ЯіІїЇєЄ]
$(document).ready(function() {
$('#name').on('keypress', function(event) {
let inputName = $("#name");
let valueName = inputName.val();
let nameKey = String.fromCharCode(event.which);
if (valueName.length === 0 && !/[a-zA-Zа-яА-ЯіІїЇєЄ]/.test(nameKey)) {
event.preventDefault();
}
});
});
// allowed chars
$('#name').on('keypress', function(e) {
let allowedChars = /[a-zA-Zа-яА-ЯёЁіІїЇєЄ0-9'\-.\_ ]/;
let charCode = (typeof e.which === "number") ? e.which : e.keyCode;
if (!allowedChars.test(String.fromCharCode(charCode))) {
e.preventDefault();
}
});
//-------------------email validation-------------------
var minLength = 8;
var maxLength = 50;
var emailRegEx = /^([a-zA-Z0-9._\-]+|[a-zA-Z0-9]+(?:[._\-][a-zA-Z0-9]+)*)@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
var input = document.querySelector('#email');
// check max len
$('#email').on('keydown keypress', function(e) {
let val = $(this).val();
if (val.length >= maxLength && e.keyCode !== 8 && e.keyCode !== 46) {
e.preventDefault();
}
});
// check deny char
function denyChar(infoCallback) {
$('#email').on('keypress', function(event) {
let regex = /[a-zA-Z0-9.\-_@]/;
let char = String.fromCharCode(event.which);
if (!regex.test(char)) {
setTimeout(function() {
event.preventDefault();
$('.valid_info_email').text('Latin letters, numbers and some symbols are allowed in this field').css('color', 'red');
setTimeout(function() {
infoCallback();
}, 1000);
}, 1000);
return false;
}
else {
infoCallback();
return true;
}
});
}
// validate email
$('#email').on('input keypress blur keydown keyup change', function() {
validateEmail();
});
let emailVal = false;
function validateEmail() {
if (input.value.length >= minLength && input.value.length <= maxLength) {
let regexFirst = /^[a-zA-Z0-9]+$/;
let regexD = /(.)\1{6}/;
// check char "@"
if (input.value.indexOf('@') !== -1) {
let email = $('#email').val();
let atIndex = email.indexOf('@');
// check dots
if (atIndex >= 0) {
let domain = email.split('@')[1];
let numDots = (domain.match(/\./g) || []).length;
// check more than 2 dots in a row after "@"
if (/@.*?\.{2,}/.test(email)) {
$('#email').removeClass('valid');
$('#email').removeClass('invalid');
$('.valid_info_email').text('The address entered is invalid. Check the address you entered').css('color', 'red');
$('#email').addClass('invalid');
let infoCallback = function() {
$('.valid_info_email').text('The address entered is invalid. Check the address you entered').css('color', 'red');
};
denyChar(infoCallback);
emailVal = false;
return false;
}
// check more than 2 dots after "@"
else if (numDots >= 3) {
$('#email').removeClass('valid');
$('#email').removeClass('invalid');
$('.valid_info_email').text('The address entered is invalid. Too many dots after "@"').css('color', 'red');
$('#email').addClass('invalid');
let infoCallback = function() {
$('.valid_info_email').text('The address entered is invalid. Too many dots after "@"').css('color', 'red');
};
denyChar(infoCallback);
emailVal = false;
return false;
}
// check first char
else if (!regexFirst.test($('#email').val().charAt(0))) {
$('#email').removeClass('valid');
$('#email').removeClass('invalid');
$('.valid_info_email').text('The address entered is invalid. The first character can only be a letter or a number').css('color', 'red');
$('#email').addClass('invalid');
let infoCallback = function() {
$('.valid_info_email').text('The address entered is invalid. The first character can only be a letter or a number').css('color', 'red');
};
denyChar(infoCallback);
emailVal = false;
return false;
}
// check duplicates
else if (regexD.test($('#email').val())) {
$('#email').removeClass('valid');
$('#email').removeClass('invalid');
$('.valid_info_email').text('The address entered is invalid. Too many identical characters').css('color', 'red');
$('#email').addClass('invalid');
let infoCallback = function() {
$('.valid_info_email').text('The address entered is invalid. Too many identical characters').css('color', 'red');
};
denyChar(infoCallback);
emailVal = false;
return false;
}
else {
// checking emailRegEx validation
if (emailRegEx.test($('#email').val())) {
// check first char
if (!regexFirst.test($('#email').val().charAt(0))) {
$('#email').removeClass('valid');
$('#email').removeClass('invalid');
$('.valid_info_email').text('The address entered is invalid. The first character can only be a letter or a number').css('color', 'red');
$('#email').addClass('invalid');
let infoCallback = function() {
$('.valid_info_email').text('The address entered is invalid. The first character can only be a letter or a number').css('color', 'red');
};
denyChar(infoCallback);
emailVal = false;
return false;
}
// check duplicates
else if (regexD.test($('#email').val())) {
$('#email').removeClass('valid');
$('#email').removeClass('invalid');
$('.valid_info_email').text('The address entered is invalid. Too many identical characters').css('color', 'red');
$('#email').addClass('invalid');
let infoCallback = function() {
$('.valid_info_email').text('The address entered is invalid. Too many identical characters').css('color', 'red');
};
denyChar(infoCallback);
emailVal = false;
return false;
}
else {
$('#email').removeClass('valid');
$('#email').removeClass('invalid');
$('.valid_info_email').text('The entered address is valid').css('color', 'green');
$('#email').addClass('valid');
let infoCallback = function() {
$('.valid_info_email').text('The entered address is valid').css('color', 'green');
};
denyChar(infoCallback);
emailVal = true;
return true;
}
}
else {
$('#email').removeClass('valid');
$('#email').removeClass('invalid');
$('.valid_info_email').text('The address entered is invalid').css('color', 'red');
$('#email').addClass('invalid');
let infoCallback = function() {
$('.valid_info_email').text('The address entered is invalid').css('color', 'red');
};
denyChar(infoCallback);
emailVal = false;
return false;
}
}
}
}
// check first char
else if (!regexFirst.test($("#email").val().charAt(0))) {
$('#email').removeClass('valid');
$('#email').removeClass('invalid');
$('.valid_info_email').text('The address entered is invalid. The first character can only be a letter or a number').css('color', 'red');
$('#email').addClass('invalid');
let infoCallback = function() {
$('.valid_info_email').text('The address entered is invalid. The first character can only be a letter or a number').css('color', 'red');
};
denyChar(infoCallback);
emailVal = false;
return false;
}
// check duplicates
else if (regexD.test($('#email').val())) {
$('#email').removeClass('valid');
$('#email').removeClass('invalid');
$('.valid_info_email').text('The address entered is invalid. Too many identical characters').css('color', 'red');
$('#email').addClass('invalid');
let infoCallback = function() {
$('.valid_info_email').text('The address entered is invalid. Too many identical characters').css('color', 'red');
};
denyChar(infoCallback);
emailVal = false;
return false;
}
else {
$('#email').removeClass('valid');
$('#email').removeClass('invalid');
$('.valid_info_email').text('The entered address is invalid, please enter "@"').css('color', 'red');
$('#email').addClass('invalid');
let infoCallback = function() {
$('.valid_info_email').text('The entered address is invalid, please enter "@"').css('color', 'red');
};
denyChar(infoCallback);
emailVal = false;
return false;
}
}
else if (input.value.length == 0) {
$('#email').removeClass('valid');
$('#email').removeClass('invalid');
$('.valid_info_email').text('');
let infoCallback = function() {
$('.valid_info_email').text('');
};
denyChar(infoCallback);
emailVal = false;
return false;
}
else {
if ((input.value.length > 0 && input.value.length < minLength) || input.value.length > maxLength) {
$('#email').removeClass('valid');
$('#email').removeClass('invalid');
$('.valid_info_email').text('The address entered is invalid, valid from ' + minLength + ' to ' + maxLength + ' characters').css('color', 'red');
$('#email').addClass('invalid');
let infoCallback = function() {
$('.valid_info_email').text('The address entered is invalid, valid from ' + minLength + ' to ' + maxLength + ' characters').css('color', 'red');
};
denyChar(infoCallback);
let regexFirst = /^[a-zA-Z0-9]+$/;
let regexD = /(.)\1{6}/;
// check first char
if (!regexFirst.test($("#email").val().charAt(0))) {
$('#email').removeClass('valid');
$('#email').removeClass('invalid');
$('.valid_info_email').text('The address entered is invalid. The first character can only be a letter or a number').css('color', 'red');
$('#email').addClass('invalid');
let infoCallback = function() {
$('.valid_info_email').text('The address entered is invalid. The first character can only be a letter or a number').css('color', 'red');
};
denyChar(infoCallback);
emailVal = false;
return false;
}
// check duplicate
else if (regexD.test($('#email').val())) {
$('#email').removeClass('valid');
$('#email').removeClass('invalid');
$('.valid_info_email').text('The address entered is invalid. Too many identical characters').css('color', 'red');
$('#email').addClass('invalid');
let infoCallback = function() {
$('.valid_info_email').text('The address entered is invalid. Too many identical characters').css('color', 'red');
};
denyChar(infoCallback);
emailVal = false;
return false;
}
emailVal = false;
return false;
}
}
}
// input parameters
$(document).ready(function() {
$('#email').on("keypress", function(event) {
let input = $("#email");
let value = input.val();
let key = String.fromCharCode(event.which);
// first character can only be [a-zA-Z0-9]
if (value.length === 0 && !/[a-zA-Z0-9]/.test(key)) {
event.preventDefault();
return false;
}
// starting from the second, you can enter [a-zA-Z0-9.-_]
if (value.length === 1 && !/[a-zA-Z0-9\-_.]/.test(key)) {
event.preventDefault();
return false;
}
// starting from the third character, you can enter [a-zA-Z0-9.\-_@]
if (value.length >= 2 && !/[a-zA-Z0-9.\-_@]/.test(key)) {
event.preventDefault();
return false;
}
// the input cannot contain two or more dots in a row (for example, '..' or '....')
if (key === "." && value.slice(-1) === ".") {
event.preventDefault();
return false;
}
// in the input, the limit on the number of consecutive identical characters is up to 5
$(document).ready(function() {
$('#email').on('input', function() {
let value = $(this).val();
let regex = /(.)\1{6}/;
if (regex.test(value)) {
$(this).val(value.slice(0, -1));
}
});
});
// after "@" the first character can only be entered [a-zA-Z0-9], starting from the second one can be entered [a-zA-Z0-9.-]
$(document).ready(function() {
$('#email').on('keypress', function(event) {
let key = String.fromCharCode(event.which);
let value = $(this).val();
// if the character "@" is entered, limit the input of the first character after "@"
if (value.indexOf("@") !== -1 && value.indexOf(".") === -1 && value.slice(-1) === "@" && !/[a-zA-Z0-9]/.test(key)) {
event.preventDefault();
return false;
}
// after the first character after "@", allow letters, numbers, dots, hyphens, or underscores
if (value.indexOf("@") !== -1 && value.indexOf(".", value.indexOf("@")+2) !== -1 && /[a-zA-Z0-9.\-_]/.test(value.slice(-1)) && !/[a-zA-Z0-9.\-_]/.test(key)) {
event.preventDefault();
return false;
}
// if a dog is found and the entered period is found after it, only the characters [a-zA-Z0-9.\-_] are allowed
if (value.indexOf("@") !== -1 && value.indexOf(".") !== -1 && value.indexOf(".") > value.indexOf("@") && !/[a-zA-Z0-9.\-_]/.test(key)) {
event.preventDefault();
return false;
}
// if a dog is found, only 2 dots can be entered after it
if (value.indexOf("@") !== -1 && value.indexOf(".", value.indexOf("@")+1) !== -1 && value.indexOf(".", value.indexOf("@")+1) !== value.lastIndexOf(".") && value.indexOf(".", value.indexOf("@")+1) !== -1 && !/[a-zA-Z]/.test(key)) {
event.preventDefault();
// if a point is found, allow input /[a-zA-Z0-9.\-_]/
if (value.indexOf("@") !== -1 && value.indexOf(".", value.indexOf("@")+1) !== -1 && value.indexOf(".", value.indexOf("@")+1) === value.lastIndexOf(".") && !/[a-zA-Z0-9.\-_]/.test(key)) {
event.preventDefault();
return false;
}
// if the second entered point is found after the dog, only characters [a-zA-Z] are allowed
if (value.indexOf("@") !== -1 && value.indexOf(".", value.indexOf("@")) !== -1 && value.indexOf(".", value.indexOf(".", value.indexOf("@")+1)) !== -1 && !/[a-zA-Z]/.test(key)) {
event.preventDefault();
return false;
}
return false;
}
});
});
// allow enter "@" only one times
$(document).ready(function() {
let inputField = $('#email');
inputField.on('keypress', function(e) {
let currentValue = $(this).val();
if (e.which === 64 && currentValue.indexOf('@') !== -1) {
e.preventDefault();
}
});
});
// allowed chars
$('#email').on('keypress', function(e) {
let allowedChars = /[a-zA-Z0-9._@-]/;
let charCode = (typeof e.which === "number") ? e.which : e.keyCode;
if (!allowedChars.test(String.fromCharCode(charCode))) {
e.preventDefault();
}
});
});
});
//-------------------textarea validation-------------------
// validate textarea
$('#message').on('blur keydown keyup change', function() {
validateTextarea();
});
let textareaVal = false;
// validation function of Textarea
function validateTextarea() {
let message = $('#message').val();
let regex_textarea = /^[a-zA-Zа-яА-ЯёЁіІїЇєЄ0-9., \-'_]+$/;
let maxLengthMessage = 500;
$('#message').on('keydown keypress', function(e) {
let val = $(this).val();
if (val.length >= maxLengthMessage && e.keyCode !== 8 && e.keyCode !== 46) {
e.preventDefault();
}
});
// checking for an empty field
if (message == 0) {
$('#message').removeClass('valid');
$('#message').removeClass('invalid');
$('.valid_info_message').text("");
textareaVal = false;
return false;
}
// if the field contains illegal characters
else if (!regex_textarea.test(message)) {
$('#message').removeClass('valid');
$('#message').removeClass('invalid');
$('.valid_info_message').text("Invalid characters entered").css('color', 'red');
$('#message').addClass('invalid');
textareaVal = false;
return false;
}
// if the field contains more than 500 characters
else if (message.length > 500) {
$('#message').removeClass('valid');
$('#message').removeClass('invalid');
$('.valid_info_message').text("The maximum message length is 500 characters").css('color', 'red');
$('#message').addClass('invalid');
textareaVal = false;
return false;
}
else {
$('#message').removeClass('valid');
$('#message').removeClass('invalid');
$('.valid_info_message').text("The entered message is valid").css('color', 'green');
$('#message').addClass('valid');
textareaVal = true;
return true;
}
}
// allowed characters
$('#message').on('keypress', function(e) {
let allowedChars = /^[a-zA-Zа-яА-ЯёЁіІїЇєЄ0-9., \-'_]/;
let charCode = (typeof e.which === "number") ? e.which : e.keyCode;
if (!allowedChars.test(String.fromCharCode(charCode))) {
e.preventDefault();
}
});
// -----------------------------------------------------------
// remove emoji
$(document).ready(function(){
var nameRegex = /^[a-zA-Zа-яА-ЯёЁіІїЇєЄ0-9.\-_ ]+$/;
var emailRegex = /^[a-zA-Z0-9._@-]+$/;
var messageRegex = /^[a-zA-Zа-яА-ЯёЁіІїЇєЄ0-9., \-'_]+$/;
$('#name').on('input keydown keyup change', function() {
removeInvalidChars($(this), nameRegex);
});
$('#email').on('input keydown keyup change', function() {
removeInvalidChars($(this), emailRegex);
});
$('#message').on('input keydown keyup change', function() {
removeInvalidChars($(this), messageRegex);
});
function removeInvalidChars(input, regex) {
var str = input.val();
str = str.replace(/[^\w\s.\-@а-яА-ЯёЁіІїЇєЄ,']/g, '');
input.val(str);
}
});
// -----------------------------------------------------------
// event handler on form submission
$('form').on('submit', function(e) {
// cancel form submission if validation fails
if (!validateName()) {
e.preventDefault();
}
else if (!validateEmail()) {
e.preventDefault();
}
else if (!validatetTextarea()) {
e.preventDefault();
}
});
// form validation
$('#contact').on('input blur keyup change', function() {
validateEmail();
validateName();
validateTextarea();
if (emailVal === true && nameVal === true && textareaVal === true) {
$('#form-submit').prop('disabled', false).attr("onclick", "window.location.href='#';");
$('#name, #email, #message').keypress(function(e) {
if (emailVal === true && nameVal === true && textareaVal === true) {
var key = e.which;
if(key == 13) {
$('#form-submit').click();
return false;
}
}
});
return true;
}
else {
$('#form-submit').prop('disabled', true);
$('#name, #email, #message').keypress(function(e) {
let key = e.which;
if(key == 13) {
return false;
}
});
return false;
}
});
});
Also see: Tab Triggers