Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 6 of 10 · Security Best Practices
Interview question

What is input validation vs sanitization, and why do you need both? Input validation और sanitization में क्या अंतर है, दोनों क्यों ज़रूरी हैं?

Answer
ConceptPurposeExample
ValidationChecks if data meets expected rules; rejects if notIs this a valid email format?
SanitizationCleans/transforms data to make it safeStrip HTML tags from a comment
// Validation - reject bad input
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    die('Invalid email');
}

// Sanitization - clean input for safe use
$comment = strip_tags($_POST['comment']);
$safeOutput = htmlspecialchars($comment, ENT_QUOTES, 'UTF-8');

Validation happens on input (accept or reject); sanitization/escaping should ideally happen on output, tailored to the context (HTML, SQL, URL, etc.) it's being used in.

Conceptउद्देश्यउदाहरण
ValidationData rules पूरे करता है या नहीं जांचताEmail valid format में है?
SanitizationData को safe बनाने के लिए साफ करता हैComment से HTML tags हटाना
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    die('Invalid email');
}

$comment = strip_tags($_POST['comment']);
$safeOutput = htmlspecialchars($comment, ENT_QUOTES, 'UTF-8');

Was this answer clear?