+ Start a Discussion
Ayyagari RameshAyyagari Ramesh 

I want to add validation to the custom vf page which i created. there are input fields which should accept only alphabets, and few input fields which should accept only numericals. could anyone suggest the best way to do it ?

Best Answer chosen by Ayyagari Ramesh
Khan AnasKhan Anas (Salesforce Developers) 
Hi Ayyagari,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:

For only Numbers:
<apex:inputField value="{!Account.Custom_Field__c}" onkeypress="return inputLimiter(event,'Numbers');" />

For only Letters:
<apex:inputField value="{!Account.Custom_Field__c}" onkeypress="return inputLimiter(event,'Letters');" />


Javascript:
function inputLimiter(e,allow) {
            var AllowableCharacters = '';

            if (allow == 'Letters'){AllowableCharacters=' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';}
            if (allow == 'Numbers'){AllowableCharacters='1234567890';}
            if (allow == 'NameCharacters'){AllowableCharacters=' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-.\'';}
            if (allow == 'NameCharactersAndNumbers'){AllowableCharacters='1234567890 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-\'';}
            if (allow == 'Currency'){AllowableCharacters='1234567890.';}

            var k = document.all?parseInt(e.keyCode): parseInt(e.which);
            if (k!=13 && k!=8 && k!=0){
                if ((e.ctrlKey==false) && (e.altKey==false)) {
                return (AllowableCharacters.indexOf(String.fromCharCode(k))!=-1);
                } else {
                return true;
                }
            } else {
                return true;
            }
        }

Please refer to below link which might help you further:

http://salesforcelearn.blogspot.com/2016/04/javascript-function-for-validation-on.html

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas