Validations on Page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pritishzin
Forum Newbie
Posts: 1
Joined: Wed Apr 01, 2009 12:13 pm

Validations on Page

Post by pritishzin »

Hello All,

I am a beginner in PHP and need to do a small application(few pages). I have a page in which I am few textbox like like FirstName, LastName, ComboBox for Birthdate. Now I have to validate this all the control and need to show error message as a listbox on the top of page or below the form.

Can you please help me how to do it. I have used Error Message list in Asp.net 2005 as i used to code in .Net and am still doing it.

Waiting for your reply.

Thanks for the help in advance.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Validations on Page

Post by McInfo »

As you validate the input, you can collect error messages in an array.

Code: Select all

<?php
$errors = array();
if (strlen($_POST['comment']) > 200)
{
    $errors[] = 'You comment should be 200 characters or less.';
}
if (0 < preg_match('/[^a-z0-9]/i', $_POST['username']))
{
    $errors[] = 'Your user name can only contain alpha-numeric characters.';
}
?>
Then you can format the errors for display on your page.

Code: Select all

<?php
if (!empty($errors))
{
    echo '<ul>';
    foreach ($errors as $error)
    {
        echo "<li>$error</li>";
    }
    echo '</ul>';
}
?>
Edit: This post was recovered from search engine cache.
Post Reply