function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kdmrkdmr 

check if value is numeric

Hi,

I am using inputtext tag to get some values in a custom visualforce page. I would like to know if we can restrict users to enter only numeric value in the input text. How do we do it? Is there any way to check if the value entered is a numeric value or not.

Thanks

KD

Best Answer chosen by Admin (Salesforce Developers) 
kdmrkdmr

Hi,

Thanks for your reply....... it is an interesting way to solve the problem, I came across a simpler way using the APEX controller extesntion class. I used the following code in the extension class to check if the value is numeric or not.

boolean check = pattern.matches('[a-zA-Z]+',stringname);

if (check == true) {

the error message you want to display

}

Thanks

KD

I used this because it was easier to use without much change to my existing codes.

 

All Answers

lvivaninlvivanin
<apex:component >
<script type="text/javascript">
function checkIsNumeric(textId)
{
   
   var textIdText =  (document.getElementById(textId).value);

   var ValidChars = "012345";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < textIdText.length && IsNumber == true; i++)
      {
      Char = textIdText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {
         IsNumber = false;
         
         }
      }
    if (IsNumber == false)
       {
       alert("Please enter a numeric value.");
       return false;
       }
   }


</script>

    <!-- Attribute Definitions -->
    <apex:attribute name="numberField" description="Number field - test description"
                    type="String" required="true"/>
    
 <table  cellpadding="0" cellspacing="10" id="theTable" style="background-color:lightblue">
 <tr>
                <td >
                <apex:outputLabel value="Test field(Enter non-numeric value and click outside...)"></apex:outputLabel>                
                </td>
                <td >                
                <apex:inputText id="inputTextID" value="{!numberField}" onblur="checkIsNumeric('{!$Component.inputTextID}');"/>
                </td>                                               
                                                                       
            </tr>
 </table>
  </apex:component>



----------------------------------------------
Test Page

<apex:page >
  <!-- Begin Default Content REMOVE THIS -->
  <apex:form >
   <apex:pageBlock >
 
  <c:NumericField id="TestId" numberField="12234"></c:NumericField>
   
  </apex:pageBlock>
</apex:form>

</apex:page>
kdmrkdmr

Hi,

Thanks for your reply....... it is an interesting way to solve the problem, I came across a simpler way using the APEX controller extesntion class. I used the following code in the extension class to check if the value is numeric or not.

boolean check = pattern.matches('[a-zA-Z]+',stringname);

if (check == true) {

the error message you want to display

}

Thanks

KD

I used this because it was easier to use without much change to my existing codes.

 

This was selected as the best answer