View Full Version : checking fields

03-08-2007, 09:46 AM

I have a form that the user fills in details.
I want to make sure only numbers are in certain fields and letters in others.
I also want to make sure all fields are filled in before a user can continue.

What's the best way of alerting a user?
Are message boxes a good idea and how can these be done in php?


03-08-2007, 01:57 PM
Here is a option for using JS


Here is the php solution

To make sure the fields aren't just blank use this:
if ($variable1 == '' || $variable2 == '' || $variable3 == '')
echo'Please fill all fields';
//process form

03-08-2007, 04:12 PM
This is how I, personally, do it:

After the user submits his input, I declare it:

$first_name = $_POST['first'];
$how_many_kids = $_POST['kids'];

Then I run checks on them with ctype_ functions. For numbers only (no decimal points or anything else), use ctype_digit(). For letters only, use ctype_alpha().

As for a message, I just throw any errors into a $Message variable and then echo it at the top of my pages. If there is no error, nothing shows up. I also specify $Error_Stat = 0 at the top of my page and change that to 1 if there is an error...and then only run the MySQL query if the $Error_Stat still equals 0.

$Error_Stat = 0;

if (!ctype_alpha($first_name)) {
$Error_Stat = 1;
$Message = 'Your first name must be only letters!';

if (!ctype_digit($how_many_kids)) {
$Error_Stat = 1;
$Message = 'Amount of kids must be only numbers!';

if ($Error_Stat == 0) {
//do the query, insert, etc. here