Page 1 of 1

Validations on Page

Posted: Wed Apr 01, 2009 12:18 pm
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.

Re: Validations on Page

Posted: Wed Apr 01, 2009 7:36 pm
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.