PostgreSQLdata validation

Validate Credit Card Number Format in PostgreSQL

Use PostgreSQL regex to validate credit card number formats. SQL examples for Visa, Mastercard, and Amex format validation.

The Regex Pattern

Regex pattern

^4[0-9]{12}(?:[0-9]{3})?$|^5[1-5][0-9]{14}$|^3[47][0-9]{13}$

Use this pattern with PostgreSQL's ~ operator.

PostgreSQL SQL Example

-- Validate credit card number format in a PostgreSQL payments table using regex ~ operator for Visa and Mastercard
SELECT *
FROM your_table
WHERE your_column ~ '^4[0-9]{12}(?:[0-9]{3})?$|^5[1-5][0-9]{14}$|^3[47][0-9]{13}$';

How This Works

Validate credit card number format in a PostgreSQL payments table using regex ~ operator for Visa and Mastercard. The regex pattern is used with PostgreSQL's ~ operator to filter rows at the database level.

Using regex directly in SQL is efficient for use cases like data validation โ€” it avoids loading data into application memory just to filter it with a regex.

Need a custom SQL + Regex query?

Describe your specific use case in plain English. RegSQL generates the exact PostgreSQL query with the right regex pattern for your schema.

Related SQL Patterns