jQuery(function ($) {
$('body').click(function () {
console.log(solution($));
});
});
function solution($) {
// debugger;
// write your code in JavaScript (Node.js 4.0.0)
//
// you can access DOM Tree using DOM Object Model:
// document.getElementById
// or using jQuery:
// $('some_tag')
//
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// NOTES
// (1) it's not clear if validation shoul fail only for ('', '') or also for ('anything', '') and ('', 'anything')
var result = false;
var form$ = $('form');
var selectedType = form$.find('input[name="type"]:checked').val();
switch (selectedType) {
case 'person':
result = ValidatePerson();
break;
case 'company':
result = ValidateCompany();
break;
default:
throw new Error('Expected a radio button with states "person" and "company".');
break; // hehe... a coding style clash ("Unreachable 'break' after 'throw'.")
}
return result;
function ValidatePersonName() {
// assuming it's valid iif not both first_name and last_name are empty
var first_name = form$.find('#first_name').val();
var last_name = form$.find('#last_name').val();
var result = (first_name + last_name).search(/\w/) > -1;
return result;
}
function ValidateEmail() {
// assuming it's valid iif not both first_name and last_name are empty
var email = form$.find('#email').val();
var pieces = email.split('@');
var result = pieces.length === 2 &&
pieces[0].search(/^[A-Za-z0-9.]{1,64}$/) === 0 &&
pieces[1].search(/^[A-Za-z0-9.]{1,64}$/) === 0;
return result;
}
function ValidateCompanyName() {
var company_name = form$.find('#company_name').val();
...
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.