Easy Input Value Validation

Or how to avoid ‘undefined’ errors

Form validation can be a giant pain in the rear, but I found a way to declare and fall back on false if the value is undefined, null or empty. This makes it so you never have to worry about compile errors crashing your script because something is ‘undefined’. The actual declaration itself is super simple, just use the OR operator for javascript as shown below.

	var x = document.getElementById('name').value || false;
	// or via jQuery
	var x = $('#name').val() || false;
	// So if an input with id="name" has no value or a value="" or does not exist, x == false
	// we can test this like so with a shorthand if statement
	x == false ? alert('Please fill in the Name field') : continueWithFormSubmission();

Let the world know