PHP US phone, US zip code and Email Validation class  [PHP]

Posted by tomfmason 1 year, 2 months ago 34 comments

This is a small validation class that I wrote for a project a long time ago. This currently has validation filters for US phone number format, US zip code format(12345 and 12345-6789 etc) and email addresses. You can pass your own custom validations to the construction like so $validation = new Validation($your_filter_array);.

 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
<?php
class Validation {
	public $default_filters = array(
		'email' =>array(
			'regex'=>'/^[^@]+@[a-zA-Z0-9._-]+\.[a-zA-Z]+$/',
			'message'=> 'is not a valid email format'
		),
		'phone' => array(
			'regex'=>'/^\(?(\d{3})\)?[-\. ]?(\d{3})[-\. ]?(\d{4})$/',
			'message' => 'is not a valid US phone number format.'
		),
		'zip' => array(
			'regex'=>'/(^\d{5}$)|(^\d{5}-\d{4}$)/',
			'message'=>'is not a valid US zipcode format.'
		)
	);
	public $filter_list = array();
	
	function Validation($filters=false) {
		if(is_array($filters)) {
			$this->filters = $filters;
		} else {
			$this->filters = array();
		}
	}
	
	function validate($filter,$value) {
		if(in_array($filter,$this->filters)) {
			if(in_array('default_filter',$this->filters[$filter])) {
				$f = $this->default_filters[$this->filters[$filter]['default_filter']];
				if(in_array('message',$this->filters[$filter])) {
					$f['message'] = $this->filters[$filter]['message'];
				}
			} else {
				$f = $this->filters[$filter];
			}
		} else {
			$f = $this->default_filters[$filter];
		}
		if(!preg_match($f['regex'],$value)) {
			$ret = array();
			$ret[$filter] = $f['message'];
			return $ret;
		}
		return true;
	}
}

//example usage
$validation = new Validation();
echo nl2br(print_r($validation->validate('phone','555-555-1212'),true));
echo nl2br(print_r($validation->validate('phone','(555)-555-1212'),true));
echo nl2br(print_r($validation->validate('phone','555 555 1212'),true));
echo nl2br(print_r($validation->validate('phone','555.555.1212'),true));
echo nl2br(print_r($validation->validate('phone','(555).555.1212'),true));
echo nl2br(print_r($validation->validate('phone','(555)---555.1212'),true));//will not match
?>

Comments

  • Comment awaiting approval

    • There are currently no comments
  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

  • Comment awaiting approval

Comment
required
required (not published)
optional