views:

1027

answers:

6

Hi have some forms that I want to use some basic php validation (regular expressions) on, how do you go about doing it? I have just general text input, usernames, passwords and date to validate. I would also like to know how to check for empty input boxes. I have looked on the interenet for this stuff but I haven't found any good tutorials.

Thanks

+3  A: 
  1. Read in the PHP.net documentation on Text processing.
  2. Use the string methods and regular expressions to make a function for email type, date type etc., returning true if valid, false otherwise.
  3. Test with the functions when submitting the form, and display the appropriate error messages to the user.
Stuart
You should almost never be writing your own email validation regular expression; the applicable specification defines a wide range of allowable formats, and you may be surprised by what is technically allowable. A simple web search should yield pre-existing "exhaustive" regexes, albeit long ones.
Rob
That depends - for my business logic email addresses like [email protected] don't make sense.
Milen A. Radev
A: 

empty input boxes are quite often not set (i'm not sure if it's standard behaviour), therefore you can do the following. client-side validation is useful in general case, but you can't be sure how these values arrive to the server, so server-side validation is a must.

if (isset($_POST['input_name'])) {
    echo $_POST['input_name'];
}

the safest way is to handle as little user input as possible. for example password, turn it into hash, you don't need to safe it in plain text anyway.

SilentGhost
A: 

I have been using Zend Framework for my last PHP projects. This might be overkill in your case, but Zend_Form is definitely worth checking out. Since Zend Framework is basically a collection of rather loosely coupled APIs you could use Zend_Form without getting into the core of ZF's MVC architecture.

cg
+1  A: 

Another function you should use is trim() to clean the input from all whitespaces for validation - some users might just enter a single whitespace as data.

Sebastian Hoitz
A: 

PHP has built in functions to deal with validation/filtering. Here's a good example of using them if they are available to you.

Brandon
A: 

There are a lot of information about it: php form.

Ginger