This is kinda driving me crazy, maybe its just one of those days.
I have this If loop that I feel like its extremely redundant but I cant find a way to make it shorter

function validateYear (valfield, infofield, required)
{
  var stat = commonCheck (valfield, infofield, required);
  if (stat != proceed) return stat;

  var tfld = trim(valfield.value);
  var yfld = trim(valfield.length);
  var Year = /^[0-9]{1,3}$/
  
  if (!Year.test(tfld)) {
    msg (infofield, "warn", "ERROR: not a valid year");
    setfocus(valfield);
    return false;
  }

  
  if (!valfield == "") {
	if (!valfield == "") {
  	if (yfld<4) { 
		msg (infofield, "warn", "ERROR: Please insert 4 digits");
		setfocus(valfield);
	} else if (yfld>4) {
		msg (infofield, "warn", "ERROR: Please insert only 4 digits");
	    setfocus(valfield);
	} else if (tfld>2020) {
	    msg (infofield, "warn", "ERROR: Please check your Data");
	    setfocus(valfield);
	}	
  	return false;
	
	} else if (!valfield == "") {
		if (tfld<1940) {
		msg (infofield, "warn", "Please check yout Data");
		} else {
		msg (infofield, "warn", "");
  }
  return true;
  }

 }
}

I'm trying to optimize that loop, repeating 3x "if (!valfield == "")" makes it look like some1 without much javascript practice made it, which is true.
I just have the feeling that same loop can be written in a more efficient way,if that makes any sense.

this should serve the purpose:

if (!valfield == "") {
		if (yfld<4) {
			msg (infofield, "warn", "ERROR: Please insert 4 digits");
			setfocus(valfield);
		} else if (yfld>4) {
			msg (infofield, "warn", "ERROR: Please insert only 4 digits");
			setfocus(valfield);
		} else if (tfld>2020) {
			msg (infofield, "warn", "ERROR: Please check your Data");
			setfocus(valfield);
		}else if (tfld<1940) {
			msg (infofield, "warn", "Please check yout Data");
		} else {
			msg (infofield, "warn", "");
		}
		
		return false;	
	} 		
  return true;

You could also try this one :

var validateYear = function( valfield, infofield, required ) {

var valfield = valfield || "";
var infofield = infofield || "";
var required = required || "";
var stat = commonCheck( valfield, infofield, required );
   if ( stat !== proceed ) {
      return stat;
   } var tfld = trim( valfield.value );
var yfld = trim( valfield.length );

var Year = ((!(/^[0-9]{1,3}$/.test( tfld ))) ? 0 : 4 ); 

var exception = ( function( errorMessage ) {
   msg( infofield, "warn", errorMessage );
   setfocus( valfield );
   return;
} );
   var error = ["ERROR: not a valid year", "ERROR: Please enter 4 digits", "Please insert only 4 digits", "ERROR: Please check your Data", 0];

var condition = (( !Year ) ? Year : (( yfld < 4 ) ? 1 : (( yfld > 4 ) ? 2 : (( tfld > 2020 || tfld < 1940 ) ? 3 : 4 )))); 
   if ( valfield.value ) {
      if ( error[ condition ] ) {
         exception( error[ condition ] );
      return false;
      } msg( infofield, "warn", "" );
      return true;
   } exception( "ERROR: Please enter some data" );
   return false;
};

I'm worried about:

var tfld = trim( valfield.value );
var yfld = trim( valfield.length );

Surely either:

var tfld = trim( valfield );
var yfld = valfield.length;

or:

var tfld = trim( valfield.value );
var yfld = tfld.length;

depending on what is passed in as valfield ?

Also what is the variable proceed ?

Airshow

This article has been dead for over six months. Start a new discussion instead.