Validate CSV data and check for formatting issues
Validate CSV files and check for errors instantly with our free online CSV validator. This tool helps data analysts and developers verify CSV structure, detect formatting issues, and ensure data integrity. Simply paste your CSV data and get instant validation feedback.
This validator checks your CSV data for common formatting issues and provides detailed feedback about errors and warnings.
CSV validator checks CSV files for proper formatting, structure, and data consistency. Verify column counts, detect encoding issues, find malformed rows, validate data types, and ensure CSV compliance before processing.
Parse CSV line by line, split by delimiter, handle quoted fields, count columns per row. Check: consistent column count, proper quote escaping, valid encoding, data type consistency. Report errors with line numbers.
CSV rules (RFC 4180): comma delimiter (or tab, semicolon), CRLF line endings, quoted fields for commas/newlines, double quotes escape quotes. Common issues: inconsistent columns, unescaped quotes, encoding (UTF-8 vs Latin-1), BOM characters.
Validate CSV before database import.
Check user data before bulk insertVerify CSV structure in data pipelines.
Validate user-uploaded CSV files.
Check data consistency and completeness.
// Comprehensive CSV validator
function validateCSV(csvContent, options = {}) {
const {
delimiter = ',',
hasHeader = true,
expectedColumns = null,
requiredHeaders = []
} = options;
const errors = [];
const warnings = [];
const lines = csvContent.split(/\r?\n/);
// Parse line respecting quotes
function parseLine(line) {
const result = [];
let current = '';
let inQuotes = false;
for (let i = 0; i < line.length; i++) {
const char = line[i];
if (char === '"') {
if (inQuotes && line[i + 1] === '"') {
current += '"';
i++;
} else {
inQuotes = !inQuotes;
}
} else if (char === delimiter && !inQuotes) {
result.push(current.trim());
current = '';
} else {
current += char;
}
}
result.push(current.trim());
return result;
}
// Validate
let headerCount = null;
let headers = [];
lines.forEach((line, index) => {
if (!line.trim()) {
if (index < lines.length - 1) {
warnings.push(`Line ${index + 1}: Empty line`);
}
return;
}
const fields = parseLine(line);
const lineNum = index + 1;
if (index === 0) {
headerCount = fields.length;
if (hasHeader) {
headers = fields;
// Check required headers
requiredHeaders.forEach(req => {
if (!headers.includes(req)) {
errors.push(`Missing required header: ${req}`);
}
});
// Check duplicate headers
const dupes = headers.filter((h, i) => headers.indexOf(h) !== i);
if (dupes.length) {
errors.push(`Duplicate headers: ${dupes.join(', ')}`);
}
}
// Check expected columns
if (expectedColumns && fields.length !== expectedColumns) {
errors.push(`Expected ${expectedColumns} columns, found ${fields.length}`);
}
} else {
// Check column count
if (fields.length !== headerCount) {
errors.push(`Line ${lineNum}: Expected ${headerCount} columns, found ${fields.length}`);
}
}
// Check for unescaped quotes
if ((line.match(/"/g) || []).length % 2 !== 0) {
errors.push(`Line ${lineNum}: Unbalanced quotes`);
}
});
return {
valid: errors.length === 0,
errors,
warnings,
stats: {
totalLines: lines.length,
dataRows: hasHeader ? lines.length - 1 : lines.length,
columns: headerCount,
headers: hasHeader ? headers : null
}
};
}
// Usage
const result = validateCSV(csvText, {
hasHeader: true,
requiredHeaders: ['id', 'name', 'email']
});
console.log(result.valid ? 'Valid CSV' : 'Invalid CSV');
result.errors.forEach(e => console.error(e));Validates CSV structure, column counts, headers, and quote handling. Returns detailed error report.
Use CSV validator before data imports, in ETL pipelines, for user file uploads, or when debugging CSV parsing issues.
Consistent column count, properly escaped quotes (doubled inside quoted fields), consistent delimiter, valid encoding. RFC 4180 defines standard, but many variations exist.