Validate E-mail and restrict domain in jQuery.
Filter e-mail address by domain name and ensure that it is filled out.
by Erik Woods
HTML
<form id="signup" method="post">
<input type="email" placeholder="Your e-mail." />
<button>Submit</button>
</form>
<footer>
<p>Example found from <a href="http://stackoverflow.com/a/12175207" target="_top">Stack Overflow</a> and modified to work for my purposes.</p>
<p><a href="http://erikwoods.com" target="_blank">Fiddle by Erik Woods</a> | <a href="http://jsfiddle.net/user/erikwoods/fiddles/" target="_blank">Other Fiddles by Erik Woods</a></p>
</footer>
CSS
button, input {
display: block;
margin: 10px;
}
footer{
background: #ff0;
border: 0 none;
border-top: 1px solid #000;
bottom: 0;
color: #000;
padding: 10px;
position: fixed;
width: 100%;
}
JavaScript
$('#signup').submit(function() {
validateEmail($('input').val());
return false;
});
function validateEmail(email) {
var re = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
if (re.test(email)) {
if (email.indexOf('@example.com', email.length - '@example.com'.length) !== -1) {
alert('Submission was successful.');
} else {
alert('Email must be from example.com ([email protected]).');
}
} else {
alert('Not a valid e-mail address.');
}
}