Validate An ID Field Format

There may be times where you would like to valid if an sObject ID is valid.    A good use case would be when an ID is passed as a URL parameter to a visualforce page.   The below method can be used to check this.

 /**
 * Validates an ID passed.
 **/ 
 public Boolean validId(String inputId) {
 String id = String.escapeSingleQuotes(inputId);
 if((id.length() == 15 || id.length() == 18) && Pattern.matches('^[a-zA-Z0-9]*$', id)) {
 return true;
 }
 return false;
 }

Leave a Reply

Your email address will not be published. Required fields are marked *