Monday, 23 March 2015

Allow only positive numbers in textbox using javascript in salesforce

            Find the below simple JavaScript code to allow only positive numbers in textbox. The below script done using javascript keyboard events. This will prevent users from entering other characters except numbers.

Example:

  1. <apex:page standardController="Account">
  2.    
  3.     <script type="text/javascript">
  4.    
  5.     function isNumber(evt)
  6.     {
  7.         evt = (evt) ? evt : window.event;
  8.         var charCode = (evt.which) ? evt.which : evt.keyCode;
  9.         if(charCode > 31 && (charCode < 48 || charCode > 57))    
  10.         {
  11.             alert('Please enter positive number only');
  12.             return false;
  13.         }
  14.         return true;
  15.     }
  16.    
  17.     </script>
  18.    
  19.     <apex:form >
  20.         <apex:pageBlock >
  21.             <apex:pageBlockSection >
  22.                 <apex:inputField value="{!Account.Name}"/>
  23.                 <apex:inputField value="{!Account.AccountNumber}" onkeypress="return isNumber(event)"/>
  24.             </apex:pageBlockSection>
  25.         </apex:pageBlock>
  26.     </apex:form>
  27.    
  28. </apex:page>

Thanks to StackExchange 

Reference: Keyboard events


No comments:

Post a Comment

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...