javascript validation in visualforce page

Using java script makes it easy to show validation messages as pop ups in a visualforce page. We can call java script function to validate the Input value and subsequently show a pop up message to indicate to the user. 

Following visualforce page uses java script to check if the user had typed Expected price after submit button is pressed.

<!-- Using java script for validation in a visualforce page -->
<apex:page standardcontroller="Account">
  <apex:form >
        <apex:pageBlock >
           Expected price  : <apex:inputText id="inptpriceID"/>
                       <apex:commandButton onclick="validateFunction('{!$Component.inptpriceID}')" value=" Submit Price"/>
  <!-- Java script starts Here -->
   function validateFunction(amountinputID){
       var inputAmount = document.getElementById(amountinputID).value;
         if(inputAmount == ''){
            alert('Please enter amount before submitting price');
 <!-- java script ends here -->  

validateFunction receives html id of the inputtext (Expected price) and from this id checks whether any input was provided, if there was no input then a alert pop is shown.

No comments:

Post a comment