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
Vijaya Kumar RegantiVijaya Kumar Reganti 

Validation on a text field using Apex

Hi All,

 

I have a picklist and a text field.for a particular picklist value I don't want to allow the input(number also) in the text field and it has to show an error message.For the other values it has to allow the input in the text field. 

 

I have written the following code for this 

 

if(objqirs.Alert_Type__c == 'NPI Launch Impact' && objqirs.Last_Approved_Program_Milestone__c == null){
NPIError = true;
return null;
}
else{

}

 

this restricts only the text values.Please help me to restrict the numbers also in Last_Approved_Program_Milestone__c field.

 

If I give number in that field, my validation will not work and the record will be saved.this should not happen.

 

Thanks,

Vijay

Best Answer chosen by Admin (Salesforce Developers) 
Vijaya Kumar RegantiVijaya Kumar Reganti

Hi,

 

I have done this in the following way

 

if((objqirs.Alert_Type__c == 'NPI Launch Impact' && objqirs.Last_Approved_Program_Milestone__c == null) || (objqirs.Alert_Type__c == 'NPI Launch Impact' && objqirs.Last_Approved_Program_Milestone__c.isNumeric())){
NPIError = true;
return null;
}
else{

}

 

it worked perfectly

 

Thanks.

All Answers

prakash_sfdcprakash_sfdc
Hi,

Try the following code:

String s = objqirs.Last_Approved_Program_Milestone__c;

if(objqirs.Alert_Type__c == 'NPI Launch Impact' || s.isNumeric() || s.isBlank() || s.isEmpty()){
NPIError = true;
return null;
}
else{

}
Bhawani SharmaBhawani Sharma
Use isNumeric function if you want to handle this on server side. Otherwise you can use some regx in javascript to handle this on client side.
Vijaya Kumar RegantiVijaya Kumar Reganti

Hi,

 

I have done this in the following way

 

if((objqirs.Alert_Type__c == 'NPI Launch Impact' && objqirs.Last_Approved_Program_Milestone__c == null) || (objqirs.Alert_Type__c == 'NPI Launch Impact' && objqirs.Last_Approved_Program_Milestone__c.isNumeric())){
NPIError = true;
return null;
}
else{

}

 

it worked perfectly

 

Thanks.

This was selected as the best answer
Vijaya Kumar RegantiVijaya Kumar Reganti

Thanks Bhavani.It worked

 

Vijaya Kumar RegantiVijaya Kumar Reganti

Thanks. It gives me an idea of using isNumeric() method.It worked.

 

Thanks a lot...

PrasadVRPrasadVR

Vijay Can you try this 

 

if(objqirs.Alert_Type__c == 'NPI Launch Impact' && (objqirs.Last_Approved_Program_Milestone__c == null || objqirs.Last_Approved_Program_Milestone__c.isNumeric())){
NPIError = true;
return null;
}
else{

}