-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvalidate.php
More file actions
42 lines (31 loc) · 1.01 KB
/
validate.php
File metadata and controls
42 lines (31 loc) · 1.01 KB
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
<?php
require_once 'classes/validate.php';
$val = new Validate;
$invalidEmail = 'foo';
$shortPassword = 'sdfsd';
$validEmail = 'foo@bar.com';
$matchVal = 'The Red car is blue';
$regexPattern = '/red/i';
/*
* First method of adding validation rules using pipes
*/
$val->rule($invalidEmail, 'Email', 'required|email'); // Will return false
$val->rule($shortPassword, 'Password', 'min_length[10]|max_length[25]');
$val->rule($validEmail, 'Email2', 'required|email'); // Will return true
$val->rule($matchVal, 'Car', "match_regex[{$regexPattern}]"); // Will return true
/*
* Second method of adding validation rules using arrays
*/
$val1 = new Validate;
$val1->ruleA($invalidEmail, 'Email', array('required','email'));
$val1->ruleA($shortPassword, 'Password', array('min_length' => 10, 'max_length' => 25));
$val1->ruleA($matchVal, 'Car', array('match_regex' => $regexPattern));
$errors = $val->errors();
if (!$errors) {
echo 'Success!';
}
else {
foreach ($errors as $error) {
echo "{$error}<br/>";
}
}