Jump to content

[SOLVED] validation of form,


zgkhoo

Recommended Posts

<html>

<body>
<form action="oldinsert.php" method="post">
<table border="1">



<tr><td>Username:</td> <td> <input type="text" name="username" /></td></tr>
<tr><td>Password:</td> <td> <input type="text" name="password" /></td></tr>
<tr><td>IC Number:</td> <td> <input type="text" name="ic" /></td></tr>
<tr><td>Full name:</td> <td><input type="text" name="fullname"/></td></tr>
<!--<tr><td>Gender:</td> <td> <input type="text" name="gender" /></td></tr>-->

<tr><td>Gender</td><td>
<select name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
</select>
</td></tr>

<tr><td>Date of Birth:</td> <td> <input type="text" name="dob" /></td></tr>
<tr><td>Address:</td><td> <input type="text" name="address" /></td></tr>
<tr><td>Postcode:</td><td> <input type="text" name="postcode" /></td></tr>
<tr><td>City:</td><td> <input type="text" name="city" /></td></tr>
<tr><td>State:</td><td> <input type="text" name="state" /></td></tr>
<tr><td>Phone Number:</td><td> <input type="text" name="phonenumber" /></td></tr>
<tr><td>H/P Number:</td><td><input type="text" name="hpnumber" /></td></tr>
<!--<tr><td>Bank type:</td><td> <input type="text" name="banktype" /></td></tr>-->

<tr><td>Bank Type:</td><td>
<select name="banktype">
<option value="maybank">Maybank</option>
<option value="publicbank">Public Bank</option>
<option value="rhb">Rhb</option>
<option value="hsbc">Hsbc</option>
</select>
</td></tr>

<tr><td>A/C Name:</td><td> <input type="text" name="accname" /></td></tr>
<tr><td>A/C Number</td><td> <input type="text" name="accnumber" /></td></tr>

<tr><td><input type="submit" /></td></tr>

</table>

</form>
</body>

</html>

 

may i know how to add in validation to this form using php?

thanks.

Link to comment
https://forums.phpfreaks.com/topic/64384-solved-validation-of-form/
Share on other sites

Validation can involve lots of stuff, starting from empty fields, nr of characters, type of characters etc. Im just giving some points then im sure ull continue by yourself.

 

empty fields:

 

if($_POST['username'] == ""){
     echo "Please fill the username field";
}

 

nr of characters

 

if(strlen($_POST['password']) > 6 or strlen($_POST['password']) < 3){
    echo "The length of the password is incorrect";
}

 

type of characters

 

if(!is_numeric($_POST['phonenumber'])){
   echo "The phone number is invalid";
}

 

also u can use advanced methods to validate fields that have a specified format, like e-mail. Use regex in these cases:

 

if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email'])) {
     echo "The e-mail is invalid"; //i suck at regex so i just copy pasted that regex (also use preg instead of eregi)
} 

 

Thats the basic idea of validation, which can be extended to a lot more. Just know what fields must be validated and how.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.