0

Thanks to all the other SO questions regarding javascript and comparing dates, I was able to come up with something that works well for my situation. Now, I just need to clear the input field if the date entered is more than 30 days old.

function ChngStatusEffective(obj)
{
  p=/(\d{1,2})\/(\d{1,2})\/(\d{1,4})/;
  if (!obj.value.match(p)) return;
  num=new Array();
  num=obj.value.match(p);
  if (num[1].length == 1) num[1]="0" + num[1];
  if (num[2].length == 1) num[2]="0" + num[2];
  if (num[3].length == 1) num[3]="200" + num[3];
  if (num[3].length == 2) num[3]="20" + num[3];
  strValue= num[1] + "/" + num[2] + "/" + num[3];

    var today = new Date ();
    var Date2 = new Date (strValue);
    var Days = Math.floor((Date2.getTime() - today.getTime())/(1000*60*60*24));
    alert('Days between two dates is: ' + Days);
    if (Days >= 0)
        {
            alert('Future dates are not allowed ' + Days);
            //  document.getElementByName('statuseffective').value();
            $('#stauseffective').val('');
            return false;
        }
    else if ((Days === -1) || (Days > -31))
        {
            alert('Date entered looks good'  + Days );
        }
    else 
        {
            alert('Are you sure you entered the correct date? ' + Days);
        }
validateUSDate( strValue )
}

The function is called:

<input type="text" name="statuseffective" onchange="ChngStatusEffective(this)" />

I need only this input field name, statuseffective to be blanked. NOTE: there could be one input field or there could be 100. I only need the one input field blanked.

LoveAndCoding
  • 7,857
  • 2
  • 31
  • 55
HPWD
  • 2,232
  • 4
  • 31
  • 61
  • this may help, working with checking if date is in a range: http://stackoverflow.com/a/1906680/158014 – Jakub Jan 27 '12 at 18:45
  • my function itself it working fine in determining if the date entered is within range, I just need to clear out the correct input box in cases where there are more than one. Each input box may have a different date so I cannot clear them all out. – HPWD Jan 27 '12 at 18:52

2 Answers2

2

Since you are passing this to the ChngStatusEffective method you can use it to set its value.

$(obj).val('');

or

obj.value = '';
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
  • Thank you. I tried both ways you suggested and both work perfectly. – HPWD Jan 27 '12 at 18:59
  • just for a tasty tidbit, how would I set the focus back to that field? I tried `$(obj).val('').focus();` but that didn't work (didn't produce any errors either). – HPWD Jan 27 '12 at 20:13
1
$('input[name="statuseffective"]').val('');

should do it, but if you have any other inputs with that name, it will clear them as well - using an id on that input and selecting it based on id would make sure no other inputs were cleared

kinakuta
  • 9,029
  • 1
  • 39
  • 48
  • Unfortunately, I can't add an id to this input field. I have to be able to determine which input field changed. What else you got? :) – HPWD Jan 27 '12 at 18:50