-
Notifications
You must be signed in to change notification settings - Fork 14
/
validate.php
65 lines (55 loc) · 2.46 KB
/
validate.php
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
<?php
/**
* validation.php
* This is the validation class that could easily be used to validate
* against a database, ie: checking if a username is taken, or if an email is already registered
* to provide real-time validation that is much more convenient than purely server-side
* validation that requires the user to submit a form first.
*
*/
$vtype = $_REQUEST['vtype'];
$vvalue = $_REQUEST['vvalue'];
$validation = new Validation;
$validation->validate($vtype, $vvalue);
class Validation
{
private $email,
$phone_CA,
$postal_CA;
function __construct()
{
$this->email = '/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})$/';
$this->phone_CA = '/^(\+?)(1?)(\-?)(\s?)(\.?)(\(?)[2-9][0-9][0-9](\)?)(\-?)(\s?)(\.?)(([2-9][2-9][2-9])|([2-9][0-9]([2-9]|0))|([2-9]([2-9]|0)[0-9]))(\-?)(\s?)(\.?)[0-9]{4}$/'; //Canadian phone numbers
$this->postal_CA = '/^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1}(\-?)(\s?)(\.?)\d{1}[A-Z]{1}\d{1}$/i'; //Canadian postal code
/* Others
$this->rNameSingle = '/^[A-Za-zéÉçÇüÜäÄëËèÈûÛÏïöÖîÎàÀáÁâÂßêÊøØåÅ\-\']+$/'; //one name/word only
$this->rName = '/^([A-Za-zéÉçÇüÜäÄëËèÈûÛÏïöÖîÎàÀáÁâÂßêÊøØåÅ\-\']+)([\sA-Za-zéÉçÇüÜäÄëËèÈûÛÏïöÖîÎàÀáÁâÂßêÊøØåÅ\-\']+)+$/'; //either one name, or any multiple of names
$this->rNameFull = '/^([A-Za-zéÉçÇüÜäÄëËèÈûÛÏïöÖîÎàÀáÁâÂßêÊøØåÅ\-\']+)\s([A-Za-zéÉçÇüÜäÄëËèÈûÛÏïöÖîÎàÀáÁâÂßêÊøØåÅ\-\'\s]+)+$/'; //at least two names, separated by a space, with unlimited middle names
*/
}
function validate($_type, $_value)
{
switch ($_type)
{
case 'phone_CA':
if($_value = preg_match($this->phone_CA, $_value))
echo 1;
else
echo "Please enter a valid phone number.";
break;
case 'email':
if($_value = preg_match($this->email, $_value))
echo 1;
else
echo "Please enter a valid email.";
break;
case 'postal_CA':
if($_value = preg_match($this->postal_CA, $_value))
echo 1;
else
echo "Please enter a valid postal code.";
break;
}
die;
}
}