There are two levels. First check the format with a regex: 15 or 18 alphanumeric characters. For 18-character IDs, the last 3 characters are a checksum of the first 15, so you can verify the checksum to catch typos and malformed IDs before they hit the API.

Context: Stack Overflow question (score 37): how to validate a Salesforce record ID before sending it to the API.