PHP Classes

File: validation_demo.php

Recommend this page to a friend!
  Classes of Olaf Lederer  >  Validate Fields Class  >  validation_demo.php  >  Download  
File: validation_demo.php
Role: Example script
Content type: text/plain
Description: An application to test the whole class
Class: Validate Fields Class
Perform common types of form field validation
Author: By
Last change: Added examples for the latest new functions
Date: 16 years ago
Size: 4,137 bytes


Class file image Download
if (isset(
$_POST['submit'])) {
$example = new Validate_fields;
$example->check_4html = true;
$example->add_text_field("Simple_text", $_POST['string'], "text", "y");
$example->add_num_field("Number", $_POST['number'], "number", "n", 0, 5);
$example->add_num_field("Number_with_decimals", $_POST['decimal'], "decimal", "n", 2);
$example->add_date_field("Euro_date", $_POST['date'], "date", "eu", "n");
$example->add_check_box("Check_box", "check", "checkbox");
$example->add_link_field("Email_address", $_POST['email'], "email");
$example->add_link_field("internet_link", $_POST['url'], "url", "n");
    if (
$example->validation()) {
$error = "All form fields are valid!"; // replace this text if you like...
} else {
$error = $example->create_msg();
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Form field validation test application</title>
<style type="text/css">
body {
    font-family: Arial, Helvetica, sans-serif;
p {
    font-size: 14px;
    line-height: 20px;
label {
    font: 14px/20px Arial, Helvetica, sans-serif;
    width: 210px;
#main {
    margin:0 auto;
    border: 1px solid #000000;
input {
<script type="text/javascript">
if (opener.location.href == document.referrer) {

<div id="main">
  <h1>Test the validation class...</h1>
  <p>I created this class to get an easy to use form field validation script. Use this class to validate your database inputs or mail forms. Of course there is much more to validate: postcodes, special strings, credit card number etc. In the first version is it (only) possible to validate simple text, numbers, (US)dates and e-mail addresses. Invalid form fields will be reported inside a detailed error message. If all form fields are valid you can use the boolean to submit your form.</p>
  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" style="padding-left:65px;">
    <p><b>Try it by yourself:</b></p>
    <label for="string">Simple text: (required, html-tags are not allowed)</label>
    <textarea name="string" cols="35" rows="3"><?php echo (isset($_POST['string'])) ? $_POST['string'] : ""; ?></textarea><br clear="all">
    <label for="number">An integer (max. digits = 5)</label>
    <input type="text" name="number" size="10" value="<?php echo (isset($_POST['number'])) ? $_POST['number'] : ""; ?>"><br clear="all">
    <label for="decimal">A number with 2 decimals</label>
    <input type="text" name="decimal" size="10" value="<?php echo (isset($_POST['decimal'])) ? $_POST['decimal'] : ""; ?>"><br clear="all">
    <label for="date">EU-date (format = dd-mm-yyyy)</label>
    <input type="text" name="date" size="10" value="<?php echo (isset($_POST['date'])) ? $_POST['date'] : ""; ?>"><br clear="all">
    <label for="check">A checkbox (required)</label>
    <input name="check" type="checkbox" id="check" value="y"<?php if (isset($_POST['check'])) echo " checked"; ?>><br clear="all">
    <label for="email">E-mail address (required)</label>
    <input type="text" name="email" size="25" value="<?php echo (isset($_POST['email'])) ? $_POST['email'] : ""; ?>">
    <br clear="all">
    <label for="url">Hyperlink (ends with: filename, /folder/val/ or querystring) </label>
    <input type="text" name="url" size="45" value="<?php echo (isset($_POST['url'])) ? $_POST['url'] : ""; ?>">
    <br clear="all">
    <label for="submit">Test the class ->&gt;</label>
    <input type="submit" name="submit" value="Start">
  <p style="color:#FF0000;padding-left:65px;"><?php echo (isset($error)) ? $error : "&nbsp;" ?></p>
<p style="margin-top:10px;">More classes and scripts on: <a href="" target="_blank"></a></p>

For more information send a message to info at phpclasses dot org.