ryxeo-glpi-git / htdocs / epack / formvalidation.php @ 5804e08b
Historique | Voir | Annoter | Télécharger (3,96 ko)
1 |
<?php
|
---|---|
2 |
//By Chris Campbell
|
3 |
//www.particletree.com
|
4 |
// Created April 27, 2005
|
5 |
//Modified June 27, 2005
|
6 |
|
7 |
|
8 |
//After the form is submitted or onblur, request the validation type
|
9 |
$validationtype = $_GET["validationtype"]; //validationtype is ajax or php |
10 |
$continueSubmit = true ; //global var if the form is valid. used only for php validationtype. |
11 |
|
12 |
switch ($validationtype) |
13 |
{ |
14 |
case 'ajax': |
15 |
ProcessAjax(); //if validationtype is ajax go to processajax function |
16 |
break;
|
17 |
case 'php': |
18 |
processPHP();//if it is php call processphp runction
|
19 |
break;
|
20 |
} |
21 |
|
22 |
function ProcessAjax() |
23 |
{ |
24 |
$required = $_GET["sRequired"];//$required holds if the field is required. will be "required" or "notrequired" |
25 |
$typecheck = $_GET["sTypeCheck"];//$typecheck holds additional requirements like email or phone |
26 |
$val = $_GET["val"]; |
27 |
|
28 |
//validateRequired checks if it is required and then sends back feedback
|
29 |
validateRequired($required,$val,$typecheck); |
30 |
|
31 |
/*check to see which typecheck (eg. email, date, etc.) was entered as the second variable in the validateMe() function
|
32 |
check the different cases passed in from the validateMe function. Send back the appropriate information*/
|
33 |
switch ($typecheck) |
34 |
{ |
35 |
case 'date': |
36 |
validateDate($val);
|
37 |
break;
|
38 |
case 'email': |
39 |
validateEmail($val);
|
40 |
break;
|
41 |
case 'phone': |
42 |
validatePhone($val);
|
43 |
break;
|
44 |
} |
45 |
} |
46 |
|
47 |
//If the url string value for validationtype was PHP, you will be validating through this server side function
|
48 |
function processPHP() |
49 |
{ |
50 |
//request the forms variables
|
51 |
$user = $_POST["user"]; |
52 |
$email= $_POST["email"]; |
53 |
global $continueSubmit; |
54 |
|
55 |
//check to see if the different form fields are valid
|
56 |
/*
|
57 |
echo "Username: ";
|
58 |
validateRequired("required", $user, "none");//validate user
|
59 |
echo "<br />";
|
60 |
|
61 |
echo "Email: ";
|
62 |
validateEmail($email) ;//validate email
|
63 |
*/
|
64 |
//if continue is not 0 then continue with the form submission
|
65 |
if ($continueSubmit) |
66 |
{ |
67 |
//submit your form
|
68 |
} |
69 |
|
70 |
} |
71 |
|
72 |
//--------------------------VALIDATION FUNCTIONS -----------------
|
73 |
|
74 |
//Function to validate if the field is required. It just checks to see if the field is empty.
|
75 |
function validateRequired($required,$val,$typecheck) |
76 |
{ |
77 |
|
78 |
// if it is required check to see if it validates
|
79 |
if ($required == "required") |
80 |
{ |
81 |
if ($val == "") |
82 |
{ |
83 |
// if val is blank then then the field is invalid
|
84 |
echo "Requis"; |
85 |
exit(); //we do not need to check for anything else so exit the validation |
86 |
} |
87 |
|
88 |
if ($val !== "" && $typecheck == "none") |
89 |
{ |
90 |
// if val is not blank and there are no further validation checks ("none") respond with a thank you for feedback
|
91 |
echo "Merci"; |
92 |
} |
93 |
} |
94 |
// if it is not required or typecheck is not none, the script will continue to validate
|
95 |
} |
96 |
|
97 |
function validateEmail($val) |
98 |
{ |
99 |
global $continueSubmit ; |
100 |
// check the email address with a regex function
|
101 |
//regular expression is from http://regexlib.com/.
|
102 |
//To learn more about them, http://www.silverstones.com/thebat/Regex.html#intro has a pretty good tutorial
|
103 |
if(ereg ("^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$", $val, $regs)) |
104 |
{ |
105 |
echo "Merci"; |
106 |
} |
107 |
else
|
108 |
{ |
109 |
$continueSubmit = false; |
110 |
echo "Email incorrect"; |
111 |
} |
112 |
} |
113 |
|
114 |
function validatePhone($val) |
115 |
{ |
116 |
global $continueSubmit ; |
117 |
// check the phone number
|
118 |
//To learn more about them, http://www.silverstones.com/thebat/Regex.html#intro has a pretty good tutorial
|
119 |
if(ereg ("^(0[1-9][0-9]{8})$", $val, $regs)) |
120 |
{ |
121 |
echo "Merci"; |
122 |
} |
123 |
else
|
124 |
{ |
125 |
$continueSubmit = false; |
126 |
echo "Numéro incorrect (10 chiffres |
127 |
par exemple 0698744401)";
|
128 |
} |
129 |
} |
130 |
|
131 |
function validateWeb($val) |
132 |
{ |
133 |
global $continueSubmit ; |
134 |
// check the web site
|
135 |
// fopen + fread ?
|
136 |
if( ereg ("^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+[.]([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$", $val, $regs)) |
137 |
{ |
138 |
echo "Merci"; |
139 |
} |
140 |
else
|
141 |
{ |
142 |
$continueSubmit = false; |
143 |
echo "Erreur"; |
144 |
} |
145 |
} |