You need to sign in to do that
Don't have an account?

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
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
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{
}
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.
Thanks Bhavani.It worked
Thanks. It gives me an idea of using isNumeric() method.It worked.
Thanks a lot...
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{
}