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
Hari nadh babu EluruHari nadh babu Eluru 

string must be 6 digits and string must be numeric only

Take a string type of variable
string pincode = '500038';
  • Pincode must be exactly 6 digits, if the no of digits not equal to 6 then print "Pincode must be 6 digits"
  • Pincode must be numeric only, if user enters alphabets then show error message "Pincode must be numeric only" 
This functionality must be implement only in if condition 
Best Answer chosen by Hari nadh babu Eluru
Suraj Tripathi 47Suraj Tripathi 47
Hi Hari nadh babu Eluru,

Try this:
string pincode='1000a';
            if(!pincode.isNumeric()){
                system.debug('Pincode must be numeric only');
            }
            if( pincode.length() != 6){
                system.debug('Pincode must be 6 digits');
            }

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and Regards,
Suraj Tripathi

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Hari,

Can you confirm if you need them as validation rules or in apex trigger ?

Thanks,
 
Hari nadh babu EluruHari nadh babu Eluru
Apex trigger
CharuDuttCharuDutt
Hii Hari 
Try Below Code
String s1 = '12345';
if(s1.isNumeric()&& s!.size()==6){
System.debug('Hiiiii'):
}

Please Mark it As Best Answer If It Helps
ThankYou
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Hari,

Please try the below trigger logic. Replace the object and field as required.
 
trigger AccountTrigger on Account (before insert,before update) {
for(Account a:Trigger.new)
{
    system.debug('into it');
    if(!(a.BillingPostalCode.isNumeric())){
       a.adderror('Pincode must be numeric only');
    }
    if(a.BillingPostalCode.length()!= 6){
        a.adderror('Pincode must be 6 digits');
    }
}
}

If this solution helps, Please mark it as best answer.

Thanks,
 
Suraj Tripathi 47Suraj Tripathi 47
Hi Hari nadh babu Eluru,

Try this:
string pincode='1000a';
            if(!pincode.isNumeric()){
                system.debug('Pincode must be numeric only');
            }
            if( pincode.length() != 6){
                system.debug('Pincode must be 6 digits');
            }

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and Regards,
Suraj Tripathi
This was selected as the best answer
CharuDuttCharuDutt
Hii Hari
Try Below Code
String s1 = '12345';
if(s1.isNumeric()&& s1.size()==6){
System.debug('Hiiiii'):
}
Please Mark it As Best Answer If It Helps
ThankYou