troubleshooting Question

Date Validation

Avatar of JohnLucania
JohnLucania asked on
JavaScript
2 Comments1 Solution462 ViewsLast Modified:
I have this form.   The number of 'Pledge Start Date' textboxes can be changed dynamically.  
Regardless, I want to make sure that right date formats are entered, i.e. 02/31/2006 shoud not be allowed.
Also, if you enter 1302006, then js can guess and put 01/30/2006 automatically.   Even after the guess, if there is invalid date entered, js returns 'Please check the date you entered on WHICH textbox'.   There are many date textboxes, so it should be good to have WHICH date was entered wrong.

Thanks much

<html>
<head>

<body onLoad="DistGA(document.EditMode.TA, document.EditMode);">
<script language="javascript">
var bTotalCalc = false;
var totalAmount = null;
var epamt = null;
var numPayments = 12;
function CalcEqualPayment(value) {
     var value = value.replace(/[$|,]/,'');
         value = parseFloat (value);
     totalAmount = value;
     epamt = parseFloat (value)/parseInt (numPayments);
}
function DistGA (totalFld, theForm) {
     var inpValue = totalFld.value;
     CalcEqualPayment (inpValue);
     totalFld.value = formatCurrency(totalAmount);
     var totalGAObj = document.getElementById ('totalGA');
     if (totalGAObj) {
          totalGAObj.innerHTML = 'Total Amount: ' + formatCurrency(totalAmount);
     }
     
     for (ix=1; ix <= numPayments; ix++) {
          var fld = eval ('document.'+theForm.name + '.GA'+ix);
          if (fld) { fld.value = formatCurrency(epamt); }
     }
     bTotalCalc = true;

}

function AddGA(theForm) {
     elems = theForm.elements;
     var totalGA = 0.0;
     var fCnt=0;

     for (var ix=0; ix < elems.length; ix++) {
           var elem = elems[ix];
           if (!elem.name) { continue; }
           if (!elem.name.match('GA')) { continue; }
           value = elem.value.replace(/[$|,]/,'');
           var diff = ( parseFloat (value) - parseFloat (epamt))* 100.;
           
           if (diff > 1.) {
                elem.value = formatCurrency(value);
                totalGA += parseFloat(value);
               fCnt++;
           }
     }
     var balance = parseFloat (totalAmount) - parseFloat(totalGA);
     //alert ('Balance ' + balance);
     var amt =  parseFloat (balance) / parseFloat (numPayments-fCnt);
     if (amt < 0) {
          alert (  'Total Contribution amount ' + totalGA +
                   '\nis greater than Total Gift Amount ' + totalAmount);
          amt = 0.0;
     }
     for (var ix=0; ix < elems.length; ix++) {
           var elem = elems[ix];
           if (!elem.name) { continue; }
           if (!elem.name.match('GA')) { continue; }
           value = elem.value.replace(/[$|,]/,'');
           var diff = ( parseFloat (value) - parseFloat (epamt))*100.;
           if (diff > 1.) {
                ;
           }
           else {
                elem.value = formatCurrency(amt);
           }
     }
}

function UpdateGA(theForm) {
     elems = theForm.elements;
     var totalGA = 0.0;
     for (var ix=0; ix < elems.length; ix++) {
           var elem = elems[ix];
           if (!elem.name) { continue; }
           if (!elem.name.match('GA')) { continue; }
           value = elem.value.replace(/[$|,]/,'');
           totalGA += parseFloat(value);
     }
     var totalGAObj = document.getElementById ('totalGA');
     if (totalGAObj) {
          totalGAObj.innerHTML = 'Total Amount: ' + formatCurrency(totalGA);
     }
}

function formatCurrency(num) {
     num = num.toString().replace(/\$|\,/g,'');
     if(isNaN(num)) num = "0";
     sign = (num == (num = Math.abs(num)));
     num = Math.floor(num*100+0.50000000001);
     cents = num%100;
     num = Math.floor(num/100).toString();
     if(cents<10)  cents = "0" + cents;
     for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) {
          num = num.substring(0,num.length-(4*i+3))+','+
                 num.substring(num.length-(4*i+3));
     }
     
     // Comment next line you do not want 2 decimal places.
     return (((sign)?'':'-') + '$' + num + '.' + cents);
     
     // UnComment next line you do not want 2 decimal places.
     //return (((sign)?'':'-') + '$' + num);
}

function ValidateRealNumber(fld) {
     if(fld.value.match(/[^0-9.]/)) {
     fld.value=fld.value.replace(/[^0-9.]/g,'');
     }
}

</script>

<script type="text/javascript" src="/CFIDE/scripts/cfform.js"></script>
<script type="text/javascript" src="/CFIDE/scripts/masks.js"></script>
<script type="text/javascript">
<!--
    function  _CF_checkEditMode(_CF_this)
    {
        //reset on submit
        _CF_error_exists = false;
        _CF_error_messages = new Array();
        _CF_error_fields = new Object();
        _CF_FirstErrorField = null;


        //display error messages and return success
        if( _CF_error_exists )
        {
            if( _CF_error_messages.length > 0 )
            {
                // show alert() message
                _CF_onErrorAlert(_CF_error_messages);
                // set focus to first form error, if the field supports js focus().
                if( _CF_this[_CF_FirstErrorField].type == "text" )
                { _CF_this[_CF_FirstErrorField].focus(); }

            }
            return false;
        }else {
            return true;
        }
    }
//-->
</script>
</head>
<body>
<form name="EditMode" action="/BringUpEditMode.cfm" method="post" onsubmit="return _CF_checkEditMode(this)">
<BR>Total Gift Amount:

<input type="text" name="TA" value="10000" onBlur="DistGA(this, this.form);">
<BR>

<BR>


Gift Amount

<input type="text" name="GA1" length="10" maxlength="10" value="$833.33" onBlur="AddGA(this.form);">
Pledge Start Date
<input type="text" name="PD1" length="10" maxlength="10" value="04/16/2006"><BR>


Gift Amount

<input type="text" name="GA2" length="10" maxlength="10" value="$833.33" onBlur="AddGA(this.form);">
Pledge Start Date
<input type="text" name="PD2" length="10" maxlength="10" value="05/16/2006"><BR>


Gift Amount

<input type="text" name="GA3" length="10" maxlength="10" value="$833.33" onBlur="AddGA(this.form);">
Pledge Start Date
<input type="text" name="PD3" length="10" maxlength="10" value="06/16/2006"><BR>


Gift Amount

<input type="text" name="GA4" length="10" maxlength="10" value="$833.33" onBlur="AddGA(this.form);">
Pledge Start Date
<input type="text" name="PD4" length="10" maxlength="10" value="07/16/2006"><BR>


Gift Amount

<input type="text" name="GA5" length="10" maxlength="10" value="$833.33" onBlur="AddGA(this.form);">
Pledge Start Date
<input type="text" name="PD5" length="10" maxlength="10" value="08/16/2006"><BR>


Gift Amount

<input type="text" name="GA6" length="10" maxlength="10" value="$833.33" onBlur="AddGA(this.form);">
Pledge Start Date
<input type="text" name="PD6" length="10" maxlength="10" value="09/16/2006"><BR>


Gift Amount

<input type="text" name="GA7" length="10" maxlength="10" value="$833.33" onBlur="AddGA(this.form);">
Pledge Start Date
<input type="text" name="PD7" length="10" maxlength="10" value="10/16/2006"><BR>


Gift Amount

<input type="text" name="GA8" length="10" maxlength="10" value="$833.33" onBlur="AddGA(this.form);">
Pledge Start Date
<input type="text" name="PD8" length="10" maxlength="10" value="11/16/2006"><BR>


Gift Amount

<input type="text" name="GA9" length="10" maxlength="10" value="$833.33" onBlur="AddGA(this.form);">
Pledge Start Date
<input type="text" name="PD9" length="10" maxlength="10" value="12/16/2006"><BR>


Gift Amount

<input type="text" name="GA10" length="10" maxlength="10" value="$833.33" onBlur="AddGA(this.form);">
Pledge Start Date
<input type="text" name="PD10" length="10" maxlength="10" value="01/16/2007"><BR>


Gift Amount

<input type="text" name="GA11" length="10" maxlength="10" value="$833.33" onBlur="AddGA(this.form);">
Pledge Start Date
<input type="text" name="PD11" length="10" maxlength="10" value="02/16/2007"><BR>


Gift Amount

<input type="text" name="GA12" length="10" maxlength="10" value="$833.33" onBlur="AddGA(this.form);">
Pledge Start Date
<input type="text" name="PD12" length="10" maxlength="10" value="03/16/2007"><BR>

<BR>

</form>

</body>
</html>


ASKER CERTIFIED SOLUTION
Pravin Asar
Principal Systems Engineer
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros