Log in

View Full Version : Need to validate form by PHP.



shakti
01-08-2009, 11:05 AM
I have made a form which have two input field.
Now I need to validate those input.
Here is the Form

<form name="form1" action="form2.php" method="post">
<input type="text" value="" name="credit" size="11" maxlength="11"/>
<input type="text" value="" name="credit2" size="11" maxlength="11"/>
<input type="submit" value="submit" />
</form>
Now I need to validate them.
As I am only 16 and completely new in PHP, I am unable to validate them.
Please give me some codes which can validate those field.
I need to set the values of those fields as-


1. Values must be digits(0123456789).
2. Maximum and minimum length is 11.
3. 2 Values must be same.
4. First 3 digit must be 017 or 018 or 019 or 011.
5. If first 3 digit is 017 then $provider = "GrameenPhone";
6. If first 3 digit is 018 then $provider = "Aktel";
7. If first 3 digit is 019 then $provider = "Banglalink";
8. If first 3 digit is 011 then $provider = "Citycell";

Please do them with php.
Make it by -
if ($_POST['credit'] != $_POST['credit2']) {
echo "Valuse are not same";
}
elseif (Please help me to code here to validate values as disired.) {
}
else {
I will handle this section.
}
Please help me by doing this.

Nile
01-08-2009, 10:32 PM
<?php
$digits = "234234929";
switch (substr($digits, 0, 3)) {
case '123':
$provider = "Hello";
break;
case '234':
$provider = "Hey";
break;
default:
$provider = "Hi";
}
echo $provider;
?>

prasanthmj
01-20-2009, 06:45 PM
Checkout the PHP form validation script here:
PHP form validation (http://www.html-form-guide.com/php-form/php-form-validation.html)

Also, it is a good idea to do the validations at the client side as well, using JavaScript(the response is immediate)
checkout this JavaScript form validation (http://www.javascript-coder.com/html-form/javascript-form-validation.phtml) script.