/**
* focus element has gotten focus,
* blur element has lost keyboard focus,
* change input element has changed
* its always best to use an existing exception/error name
* but I could not find a good match
* https://www.w3schools.com/js/js_errors.asp
*/
$("fieldset input").change( function() {
console.log("In change: " + this.id );
testFormCompleteness( this );
//if you don't want this pointer just use
//.change( testFormCompleteness );
} );
function testFormCompleteness( e ) {
try {
//we are passing acres, ant months to create recommendation
let a = $( "input#acres2" ).val();
let m = $( "input#months2" ).val();
/* drop acres2, and month2 in js04 */
if( ! /^\d+$/.test( a ) )
throw "Acres2 type number needs positive integer: " + a;
if( ! /^(1[012]|[1-9])$/.test( m ) )
throw "Months2 type number needs 1..12 only: " + m;
a = $( "input#acres" ).val();
if( ! /^\d+$/.test( a ) )
throw "needs to be a positive number: "
+ a;
m = $("input#months").val();
/*multiple throws give you more meaniful error
but one long if probably cleaner code?
! /^(1[012]|[1-9])$/.test( m ) */
if (!m || isNaN(m)) throw "number of months needs to be 1..12";
if (m < 1) throw "months too Low " + m + " Need at least 1";
if (m > 12) throw "months too high " + m + " limit 12months/year";
createRecommendation( a, m );
} catch (err) {
$("#messageHead").text("Error").css({ color: "red" });
$("#message").text(err);
}
} //end testFormCompleteness
function createRecommendation( a, m ) {
$( "#messageHead" ).text( "Data Okay" ).css( "color", "black" );
$( "#message" ).text( "acres: " + a + " month: " + m );
}
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.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
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!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.