function Validate() {
// first we clear any left over error messages
$('#error p').remove();
// store the error div, to save typing
var error = $('#error');
// we start by assuming everything is correct
// if it later turns out not to be, we just set
// this to false
var correct = true;
// we can't tell much about valid names, so we
// just make sure the field isn't empty
if ($('#first').val() == '') {
error.append('<p>No first name provided</p>');
correct = false;
}
// ditto for last name
if ($('#last').val() == '') {
error.append('<p>No last name provided</p>');
correct = false;
}
// emails we can guess more about, so grab a copy
var email = $('#email').val();
// first we check if the email field is empty
if (email == '') {
error.append('<p>No email provided</p>');
correct = false;
}
// if it isn't empty, we check for an @ sign
else if (email.indexOf('@') == -1) {
error.append('<p>Email does not contain @ symbol</p>');
correct = false;
}
// if it has an @ sign, we need to check for a dot
// after the @ sign
else if (email.lastIndexOf('.') < email.indexOf('@')) {
error.append('<p>Email does not contain a . (full stop)</p>');
correct = false;
}
// zip codes we know have to be 5 digits, so check the length
if ($('#zip').val().length != 5) {
error.append('<p>Zip code must be 5 digits</p>');
correct = false;
}
// if we haven't found an error, we hide the error message
if (correct) {
error.css('display', 'none');
// clear the result div
$('#result').empty();
// and put together a result message
$('#result').append('<p>Hello, ' + $('#title').val() + ' ' + $('#last').val() + '!</p>');
}
// otherwise, we show the error
else {
error.css('display', 'block');
}
return...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.