Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2009-04-29
4
Medium Priority
?
680 Views
Last Modified: 2013-11-24
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

0
Comment
Question by:Kadhiravan
  • 2
2 Comments
 
LVL 27

Accepted Solution

by:
BigRat earned 1500 total points
ID: 24269076
The display field of the style attribute does not take values "true" nor "false" but a string saying how the display is to be done. The legal values are :-

block Object is rendered as a block element.
none Object is not rendered.
inline Default. Object is rendered as an inline element sized by the dimensions of the content.
inline-block Object is rendered inline, but the contents of the object are rendered as a block element. Adjacent inline elements are rendered on the same line, space permitting.
list-item   Internet Explorer 6 and later. Object is rendered as a block element, and a list-item marker is added.
table-header-group Table header is always displayed before all other rows and row groups, and after any top captions. The header is displayed on each page spanned by a table.  
table-footer-group Table footer is always displayed after all other rows and row groups, and before any bottom captions. The footer is displayed on each page spanned by a table.  

Your routine tests each element and sums them all up. I'd enhance it to count the number of errors and return them. It will then be quite easy to decide whether the input should be submitted or not.
0
 
LVL 27

Expert Comment

by:BigRat
ID: 24923623
It may no longer be vaild, but my posting is an answer to the question as to why the code does not work.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question