kreoton web development

PHP form validation class

Are you bored to write long script to validate you user input? If yes I coded a user input validation PHP class. It’s very flexible and easy to use. This class can validate user e-mail, homepage url, text and other input formats.

Class features:

  • validate and return entered one word (perfect for username and password validation, you can write custom regex for allowed chars, by default it allows only letters);
  • validate and return entered text (may return formated text, strips tags or changes special HTML characters like <, >, & and others to character codes);
  • validate and return integer number input;
  • validate and return float number input;
  • validate and return email input;
  • validate and return www address input;
  • validate and return password and password confirm match input;
  • every form field may have required option;
  • for every form field you may write custom error message;

Example of using validation class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
 
<?php
require('validate.class.php');
$VD = new validate();
 
if ($_POST['form_submit'])
{
 
	$VD->validate_data('username', $_POST['username'], array(
			'isOneWord'=>array(
				'min_chars'=>5,
				'max_chars'=>20,
				'regex'=>"/^[a-zA-Z0-9_\.]{1,}$/",
				'required'=>true)
				),
			'Username not valid'
		);
 
	$VD->validate_data('password', $_POST['password'], array(
			'isOneWord'=>array(
				'min_chars'=>6,
				'max_chars'=>15,
				'regex'=>"/^[a-zA-Z0-9_\.]{1,}$/",
				'required'=>true)
				),
			'Password not valid'
		);
	$VD->validate_data('email', $_POST['email'], array(
			'isEmail'=>array(
				'required'=>true)
				),
			'Email not valid'
		);
	$VD->validate_data('details', $_POST['details'], array(
			'isText'=>array(
				'strip'=>true,
				'required'=>true)
				),
			'Details not valid'
		);
	$VD->validate_data('age', $_POST['age'], array(
			'isInteger'=>array(
				'required'=>true
			)),
			'You must fill in your age');
 
	if (count($VD->errors) > 0)
	{
		echo 'Please fix following errors:';
		foreach ($VD->errors as $error)
		{
			echo $error.'<br />';
		}
	}
	else
	{
		$link = mysql_connect($host, $user, $pass);
		mysql_select_db($name, $link);
 
		mysql_query("INSERT INTO users VALUES (
			'".$VD->return['username']."', 
			'".md5($VD->return['password'])."', 
			'".$VD->return['email']."', 
			'".$VD->return['details']."', 
			".$VD->return['age']."
		)");
	}
}
 
?>

Download form validation class

4 Responses to “PHP form validation class”


  1. Hello! Good Site! Thanks you! ohfbeukhesuuu


  2. how to use this class ? can you create simple code (login form) and use your class .. thx’s


  3. Nice work :-)

    Just some tips:

    - For something to be considered a class or object, it should have data members and methods. You should best decompose the code into a couple of methods, and have private data members to store the state of the class.

    - And as a general rule of thumb, a method should be no longer than about 25 lines. More than that may indicate problems with the design.

    Hope that helps!


  4. Great code, i agree that is not a proper class but you can still use simply as functions. Five stars from me!!!

    Cheers

Leave a Reply

This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)

You must read and type the 5 chars within 0..9 and A..F, and submit the form.

  

Oh no, I cannot read this. Please, generate a