<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<style type="text/css">
#contact form {
width: 98%;
max-width: 600px;
height: auto;
}
#contact input, button, textarea {
font: 16px Verdana, sans-serif;
position: relative;
margin: 0.25rem 0;
padding: 2px 4px;
width: 48%;
width: calc(50% - 14px);
}
#contact .left {
float: left;
}
#contact .right {
float: right;
}
#contact textarea {
resize: none;
overflow: auto;
height: 165px;
width: 99%;
width: calc(100% - 10px);
}
.hidden {
display: none;
}
.error {
background-color: rgb(255, 255, 170);
opacity:0.8;
}
</style>
<title>Test Page</title>
</head>
<body>
<section id="contact">
<h1>Contact Me</h1>
<div id="errors" class="hidden"><small>* Your message was not sent.<br>Please revise the highlighted fields.</small></div>
<form name="contactme" id="contact-form" method="post" action="form.php" onSubmit=" return submitContactForm();">
<input class="name left" type="text" name="name" maxlength="50" class="line" value="name" onfocus="if (this.value=='name') this.value='';" onblur="if (this.value=='') this.value='name';"/>
<input class="email right" type="text" name="email" maxlength="50" class="line" value="email" onfocus="if (this.value=='email') this.value='';" onblur="if (this.value=='') this.value='email';"/>
<textarea name="comments" maxlength="1000" class="message" onfocus="if (this.value=='message') this.value='';" onblur="if (this.value=='') this.value='message';"/>message</textarea>
<button class="right" type="submit" value="Submit" >Submit</button>
</form>
</section>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
validateEmail = function(email){
var pattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
return pattern.test(email);
}
submitContactForm = function () {
var form = $("#contact-form");
var postData = {};
postData.name = form.find('input.name').val();
postData.email = form.find('input.email').val();
postData.message = form.find('textarea.message').val();
var errors = new Array();
if (postData.name == null || postData.name == '' || postData.name == 'name') {
errors.push('Name is required.');
$("input.name").addClass("error");
}
if (postData.email == null || postData.email == '' || postData.email == 'email') {
$("input.email").addClass("error");
errors.push('Please enter a valid email.');
} else if (!validateEmail(postData.email)) {
$("input.email").addClass("error");
errors.push('Please enter a valid email.');
}
if (postData.message == null || postData.message == '' || postData.message == 'message') {
$("textarea.message").addClass("error");
errors.push('Message is required.');
}
if (errors.length > 0) {
$("#errors").show();
return false;
}
}
</script>
</body>
</html>