Link to home
Start Free TrialLog in
Avatar of Kadhiravan
KadhiravanFlag for India

asked on

Render an outputText and disable a button if the value is non-numeric

I have a JSF page which contains dynamic textboxes to get the inputs from the user.
Only numeric values has to be accepted while submitting the form; else an error message should be displayed on the same page.

My requirement now is, I have javascript to validate the user Input. If the user input is a non-numeric, then the "Submit" button has to be disabled and an error messsage (using <h:outputText>) has to be displayed.
If the user Input is numeric then the error message should be displayed and the button should be enabled. The default values: for button (enabled), error message ( disabled)

I want this to be done on a single javascript call. Please find below the javascript am using. Can someone help me on this?

function sumEntries(idTotalInvestments, idTotalAssetClasses, idTotalAllocation, idAllocationAssetClasses)
{      
 //investment size is not really required, but better to have it rather than using an arbitrary number in the loop
 var investmentSize = document.getElementById(idTotalInvestments).value;
 var assetsize = document.getElementById(idTotalAssetClasses).value;      
 var actualVal=0;
                 
 for(var y=0;y < assetsize; y++)
 {
  for ( var x=0; x < investmentSize; x++)
  {
   var datalist = idAllocationAssetClasses+':'+y+':allocationInvestments:'+x+':amount';
       if(document.getElementById(datalist))
   {
        var userInput = document.getElementById(datalist).value;
    
    if(userInput.length > 0)
    {
     if(userInput.search(/[^\d\s]/) == -1)
     {
      actualVal=actualVal+parseInt(userInput);
      document.getElementById(idNonNumericValidation).style.display='false';
     }
     else
     {
      actualVal = "Error";
      document.getElementById(idNonNumericValidation).style.display='true';
      break;
     }    
    }
          }
   else
   {
    //this break is done to avoid having investment size at asset class level
        break;
   }
  }
  if(actualVal == "Error")
  {
   break;
  }
 }
 document.getElementById(idTotalAllocation).value = actualVal;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of BigRat
BigRat
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It may no longer be vaild, but my posting is an answer to the question as to why the code does not work.