Practice 10

Main | Arrays | Practice 8 | Solution 8 | Associative Arrays | Practice 9 | Solution 9 | Regular Expressions | Practice 10 | Solution 10 | More Regular Expressions | Practice 11 | Solution 11

This practice will build on the ideas that we used to create online forms. In our previous examples we checked if the user forget to fill out a part of our form by using the function empty. In this example we will also check if the user filled in data that is reasonable from syntactical point of view.

Here is the form that you will be creating for this exercise. Everything about this form is done for you except function check_input() which you must implement. Within check_input() there is a call to the function url_good() which has already been done for you. If you have a look at it you can see that it uses a regular expression to capture the general properties of any URL.

You should use ereg to check for a valid email address and a valid date and then append to the error string if necessary.

Email Address

Creating a regular expression to capture an email address will require the user of qunatifiers and the specification of correct address. Here is some guidance to help you:

  • contains an at sign ("@") after at least one or more characters
  • after the at sign there should be at least two more characters separated by a period (".").
  • valid characters inside of the words include the following
    • Uppercase Letters
    • Lowercase Letters
    • Numbers
    • underscore ("_")
    • dash ("-")
    Keep in mind that the above are symbols that you want to capture. They are not necessarily the symbols that you to use in your regular expression.

Date

In this case a valid date has this format:

YYYY-MM-DD

Such that today's date would be

This date format is what MySQL expects as its default date so checking for this type of date format if you are using PHP with MySQL.

Requirements

To complete this exercise you must:

  • Create a file in "public_html/php" called "regex_form.php".
  • Be sure that "regex_form.php" is viewable from the web.
  • Fill in "regex_form.php" with this incomplete code.

Note that there are a lot of functions in the sample code, but you only need to find and complete the function check_input() assume that the rest of the code works.


jfulton [at] member.fsf.org
22 Aug 2013