js/decklist/main.js
// validation object which is used to generate tooltip and status box text
validation object which is used to generate tooltip and status box text
validation object which is used to generate tooltip and status box text
function validateInput() {
  validate = {
    'firstname': [],
    'lastname': [],
    'dcinumber': [],
    'event': [],
    'eventdate': [],
    'eventlocation': [],
    'deckmain': [],
    'deckside': []
  };
  if ($('#firstname').val() === '')           { validate.firstname.push({'warning': 'blank'});  }
  else if ($('#firstname').val().length > 20) { validate.firstname.push({'error': 'toolarge'}); }
  if ($('#lastname').val() === '')           { validate.lastname.push({'warning': 'blank'});  }
  else if ($('#lastname').val().length > 20) { validate.lastname.push({'error': 'toolarge'}); }
  if ($('#dcinumber').val() === '')                 { validate.dcinumber.push({'warning': 'blank'});  }
  else if (!$('#dcinumber').val().match(/^[\d]+$/)) { validate.dcinumber.push({'error': 'nonnum'});   }
  else if ($('#dcinumber').val().length >= 11)      { validate.dcinumber.push({'error': 'toolarge'}); }
  else if (!DCI.isValid($('#dcinumber').val()))     { validate.dcinumber.push({'error': 'invalid'});  }
  else {
    if (DCI.isValid($('#dcinumber').val()) == -1)   { validate.dcinumber.push({'warning': 'nocheck'});}
    if (DCI.wasChanged($('#dcinumber').val()))      { validate.dcinumber.push({'warning': 'changed'});}
  }
  if ($('#event').val() === '') { validate.event.push({'warning': 'blank'}); }
  if ($('#eventdate').val() === '')                               { validate.eventdate.push({'warning': 'blank'});      }
  else if (!$('#eventdate').val().match(/^\d{4}\-\d{2}\-\d{2}$/)) { validate.eventdate.push({'error': 'unrecognized'}); }
  else if (Date.parse($('#eventdate').val()) <= new Date(new Date().setDate(new Date().getDate()-1)).setHours(0)) {
    validate.eventdate.push({'warning': 'futuredate'});
  }
  if ($('#eventlocation').val() === '') { validate.eventlocation.push({'warning': 'blank'}); }
  if ((maindeck_count == 0) || (maindeck_count > 60)) { validate.deckmain.push({'warning': 'size'});   }
  else if (maindeck_count < 60)                       { validate.deckmain.push({'error': 'toosmall'}); }
  if (maindeck.length > 44)                           { validate.deckmain.push({'error': 'toolarge'}); }
  if (sideboard_count > 15) { validate.deckside.push({'error': 'toolarge'});   }
  if (sideboard_count < 15) { validate.deckside.push({'warning': 'toosmall'}); }
  mainPlusSide = mainAndSide();
  excessCards = [];
  allowedDupes = ['Plains', 'Island', 'Swamp', 'Mountain', 'Forest',
    'Snow-Covered Plains', 'Snow-Covered Island', 'Snow-Covered Swamp', 'Snow-Covered Mountain',
    'Snow-Covered Forest', 'Wastes', 'Relentless Rats', 'Shadowborn Apostle'];
  for (i = 0; i < mainPlusSide.length; i++) {
    if (parseInt(mainPlusSide[i][1]) > 4) {
      allowed = false;
      allowedDupes.forEach(function(element, index, array){
        allowed = allowed || element === mainPlusSide[i][0];
      });
      if (!allowed) { excessCards.push(mainPlusSide[i][0]); }
    }
  }
  if (excessCards.length) { validate.deckmain.push({'error': 'quantity'}); }
  unrecognizedCards = {};
  unparseableCards = [];
  if (Object.getOwnPropertyNames(unrecognized).length !== 0) {
    unrecognizedCards = unrecognized;
    validate.deckmain.push({'warning': 'unrecognized'});
  }
  if (unparseable.length !== 0) {
    unparseableCards = unparseable;
    validate.deckmain.push({'warning': 'unparseable'});
  }
  statusAndTooltips(validate);
}
function validateInput ( ) { validate = { 'firstname' : [ ] , 'lastname' : [ ] , 'dcinumber' : [ ] , 'event' : [ ] , 'eventdate' : [ ] , 'eventlocation' : [ ] , 'deckmain' : [ ] , 'deckside' : [ ] } ; if ( $ ( '#firstname' ) . val ( ) === '' ) { validate . firstname . push ( { 'warning' : 'blank' } ) ; } else if ( $ ( '#firstname' ) . val ( ) . length > 20 ) { validate . firstname . push ( { 'error' : 'toolarge' } ) ; } if ( $ ( '#lastname' ) . val ( ) === '' ) { validate . lastname . push ( { 'warning' : 'blank' } ) ; } else if ( $ ( '#lastname' ) . val ( ) . length > 20 ) { validate . lastname . push ( { 'error' : 'toolarge' } ) ; } if ( $ ( '#dcinumber' ) . val ( ) === '' ) { validate . dcinumber . push ( { 'warning' : 'blank' } ) ; } else if ( ! $ ( '#dcinumber' ) . val ( ) . match ( / ^[\d]+$ / ) ) { validate . dcinumber . push ( { 'error' : 'nonnum' } ) ; } else if ( $ ( '#dcinumber' ) . val ( ) . length >= 11 ) { validate . dcinumber . push ( { 'error' : 'toolarge' } ) ; } else if ( ! DCI . isValid ( $ ( '#dcinumber' ) . val ( ) ) ) { validate . dcinumber . push ( { 'error' : 'invalid' } ) ; } else { if ( DCI . isValid ( $ ( '#dcinumber' ) . val ( ) ) == - 1 ) { validate . dcinumber . push ( { 'warning' : 'nocheck' } ) ; } if ( DCI . wasChanged ( $ ( '#dcinumber' ) . val ( ) ) ) { validate . dcinumber . push ( { 'warning' : 'changed' } ) ; } } if ( $ ( '#event' ) . val ( ) === '' ) { validate . event . push ( { 'warning' : 'blank' } ) ; } if ( $ ( '#eventdate' ) . val ( ) === '' ) { validate . eventdate . push ( { 'warning' : 'blank' } ) ; } else if ( ! $ ( '#eventdate' ) . val ( ) . match ( / ^\d{4}\-\d{2}\-\d{2}$ / ) ) { validate . eventdate . push ( { 'error' : 'unrecognized' } ) ; } else if ( Date . parse ( $ ( '#eventdate' ) . val ( ) ) <= new Date ( new Date ( ) . setDate ( new Date ( ) . getDate ( ) - 1 ) ) . setHours ( 0 ) ) { validate . eventdate . push ( { 'warning' : 'futuredate' } ) ; } if ( $ ( '#eventlocation' ) . val ( ) === '' ) { validate . eventlocation . push ( { 'warning' : 'blank' } ) ; } if ( ( maindeck_count == 0 ) || ( maindeck_count > 60 ) ) { validate . deckmain . push ( { 'warning' : 'size' } ) ; } else if ( maindeck_count < 60 ) { validate . deckmain . push ( { 'error' : 'toosmall' } ) ; } if ( maindeck . length > 44 ) { validate . deckmain . push ( { 'error' : 'toolarge' } ) ; } if ( sideboard_count > 15 ) { validate . deckside . push ( { 'error' : 'toolarge' } ) ; } if ( sideboard_count < 15 ) { validate . deckside . push ( { 'warning' : 'toosmall' } ) ; } mainPlusSide = mainAndSide ( ) ; excessCards = [ ] ; allowedDupes = [ 'Plains' , 'Island' , 'Swamp' , 'Mountain' , 'Forest' , 'Snow-Covered Plains' , 'Snow-Covered Island' , 'Snow-Covered Swamp' , 'Snow-Covered Mountain' , 'Snow-Covered Forest' , 'Wastes' , 'Relentless Rats' , 'Shadowborn Apostle' ] ; for ( i = 0 ; i < mainPlusSide . length ; i ++ ) { if ( parseInt ( mainPlusSide [ i ] [ 1 ] ) > 4 ) { allowed = false ; allowedDupes . forEach ( function ( element , index , array ) { allowed = allowed || element === mainPlusSide [ i ] [ 0 ] ; } ) ; if ( ! allowed ) { excessCards . push ( mainPlusSide [ i ] [ 0 ] ) ; } } } if ( excessCards . length ) { validate . deckmain . push ( { 'error' : 'quantity' } ) ; } unrecognizedCards = { } ; unparseableCards = [ ] ; if ( Object . getOwnPropertyNames ( unrecognized ) . length !== 0 ) { unrecognizedCards = unrecognized ; validate . deckmain . push ( { 'warning' : 'unrecognized' } ) ; } if ( unparseable . length !== 0 ) { unparseableCards = unparseable ; validate . deckmain . push ( { 'warning' : 'unparseable' } ) ; } statusAndTooltips ( validate ) ; }
validation object which is used to generate tooltip and status box text
validation object which is used to generate tooltip and status box text
// validation object // key = HTML form object (input or textarea) ID // value = array of error objects: {error_level: error_type} // error levels include 'warning' and 'error' // error types include 'blank', 'nonnum', 'toolarge', 'toosmall', //       'size', 'unrecognized', 'quantity', 'futuredate' // check first name (non-blank, too long) // check last name (non-blank, too long) // check DCI number (non-blank, numeric, < 11 digits, valid, has check digit, was changed) // check event name (non-blank) // check event date (non-blank, unrecognized format, before today) // check event location (non-blank) // check maindeck (size, number of unique cards) // check sideboard (size) // check combined main/sb (quantity of each unique card, unrecognized cards) // pass validation data to output status/tooltip information