PHP Form Validation
In this page:
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
// 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";
}
?>
Login to try C/C++/Java/PHP code in the editor
Integers Validate करना
empty() और isset() required fields के लिए defense की पहली line हैं, आपको तुरंत reject करने देते हुए अगर email address जैसा एक mandatory field missing या blank हो।
उदाहरण: Validating Integers
<?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";
}
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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));
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
}
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
}
?>
Login to try C/C++/Java/PHP code in the editor
issetके बिनाif ($_POST[x])इस्तेमाल करना, जो field missing होने पर एक warning produce करता है।- सिर्फ client-side validation पर भरोसा करना, जबकि यह bypass किया जा सकता है।
filter_varइस्तेमाल करना और इसके result को loose check से test करना, ताकि0को failure माना जाए।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: