← Back to PHP Course | Chapter 10: Forms & Validation | Lesson 2 of 8

PHP Form Validation

Form validation किसी teacher के accept करने से पहले यह check करने जैसा है कि homework sheet सही से भरी है। PHP check करता है कि required fields खाली नहीं हैं और emails जैसी चीज़ें सही दिखती हैं।
Syntax
php
if (filter_var($value, FILTER_VALIDATE_EMAIL)) { }
if (filter_var($value, FILTER_VALIDATE_INT)) { }
if (empty($value)) { }
if (strlen($value) > max_length) { }

Emails Validate करना

Server-side validation check करता है कि submitted form data actually आपकी requirements पूरी करता है या नहीं — सही format, required fields मौजूद, range के अंदर values — और यह हमेशा करना ज़रूरी है भले ही आप JavaScript में भी validate करें, क्योंकि client-side checks पूरी तरह bypass किए जा सकते हैं।

उदाहरण: Validating Emails

php
<?php
// Declare `$email`, set to "[email protected]"
$email = "[email protected]";
// Check whether `filter_var($email, FILTER_VALIDATE_EMAIL)`
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // Print "Valid email" to the output
    echo "Valid email";
// Otherwise, run this branch
} else {
    // Print "Invalid email" to the output
    echo "Invalid email";
}
?>

Integers Validate करना

empty() और isset() required fields के लिए defense की पहली line हैं, आपको तुरंत reject करने देते हुए अगर email address जैसा एक mandatory field missing या blank हो।

उदाहरण: Validating Integers

php
<?php
// Set `$_POST['email']` to ""
$_POST['email'] = "";
// Check whether `empty($_POST['email'])`
if (empty($_POST['email'])) {
    // Print "Email is required" to the output
    echo "Email is required";
}
?>

Input Length Check करना

filter_var($value, FILTER_VALIDATE_EMAIL) और similar filter_var() calls emails और URLs जैसे common formats के लिए built-in validation देते हैं, आपको हाथ से fragile validation logic लिखने से बचाते हुए।

उदाहरण: Checking Input Length

php
<?php
// Declare `$email`, set to "[email protected]"
$email = "[email protected]";
// Print a detailed dump (with types) of `filter_var($email, FILTER_VALIDATE_EMAIL)`
var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));
?>

Custom Format Validation

Numeric fields को is_numeric() plus range checks जैसे explicit checks चाहिए, क्योंकि PHP की loose typing का मतलब है कि 5abc जैसी एक string arithmetic में unpredictably behave कर सकती है अगर आप पहले इसका shape validate न करें।

उदाहरण: Custom Format Validation

php
<?php
// Declare `$age`, set to "25abc"
$age = "25abc";
// Check whether `is_numeric($age) && $age > 0`
if (is_numeric($age) && $age > 0) {
    // Print "Valid" to the output
    echo "Valid";
// Otherwise, run this branch
} else {
    // Print "Invalid numeric input" to the output
    echo "Invalid numeric input";
}
?>

Validation Errors Collect करना

अच्छा validation एक single generic 'invalid input' message के बजाय specific, actionable feedback देता है (जैसे 'Password must be at least 8 characters'), जो users को यह पता लगाने की कोशिश में frustrate करता है कि असल में क्या गलत हुआ।

उदाहरण: Collecting Validation Errors

php
<?php
// Declare `$password`, set to "abc"
$password = "abc";
// Check whether `strlen($password) < 8`
if (strlen($password) < 8) {
    // Print "Password must be at least 8 characters" to the output
    echo "Password must be at least 8 characters";
}
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. isset के बिना if ($_POST[x]) इस्तेमाल करना, जो field missing होने पर एक warning produce करता है।
  2. सिर्फ client-side validation पर भरोसा करना, जबकि यह bypass किया जा सकता है।
  3. filter_var इस्तेमाल करना और इसके result को loose check से test करना, ताकि 0 को failure माना जाए।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.