inputfield-checker
Code (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
function inputCheck($values, $names)
{
$errs = array();
foreach($values as $key => $value)
{
switch($value)
{
case 'value':
if(empty($_POST[$key]))
{
$errs[] = $names[$key];
}
break;
case 'email':
if(empty($_POST[$key]))
{
$errs[] = $names[$key];
}
else
{
if (ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.'@'.'[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.'[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$', $_POST[$key]))
{
if ($check_domain && function_exists('checkdnsrr'))
{
list (, $domain) = explode('@', $_POST[$key]);
if (checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A'))
{
return true;
}
else
{
$errs[] = $names[$key];
}
}
}
else
{
$errs[] = $names[$key];
}
}
break;
case 'numeric':
if(!ctype_digit($_POST[$key]))
{
$errs[] = $names[$key];
}
break;
case 'equal':
$singleKey = explode(',', $key);
if(empty($_POST[$singleKey[0]]))
{
$errs[] = $names[$singleKey[0]];
}
elseif(empty($_POST[$singleKey[1]]))
{
$errs[] = $names[$singleKey[1]];
}
else
{
if($_POST[$singleKey[0]] != $_POST[$singleKey[1]])
{
$errs[] = $names[$singleKey[0]].' & '.$names[$singleKey[1]].' komen niet overeen';
}
}
break;
default:
if(empty($_POST[$key]))
{
$errs[] = $names[$key];
}
}
}
return $errs;
}
?>
function inputCheck($values, $names)
{
$errs = array();
foreach($values as $key => $value)
{
switch($value)
{
case 'value':
if(empty($_POST[$key]))
{
$errs[] = $names[$key];
}
break;
case 'email':
if(empty($_POST[$key]))
{
$errs[] = $names[$key];
}
else
{
if (ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.'@'.'[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.'[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$', $_POST[$key]))
{
if ($check_domain && function_exists('checkdnsrr'))
{
list (, $domain) = explode('@', $_POST[$key]);
if (checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A'))
{
return true;
}
else
{
$errs[] = $names[$key];
}
}
}
else
{
$errs[] = $names[$key];
}
}
break;
case 'numeric':
if(!ctype_digit($_POST[$key]))
{
$errs[] = $names[$key];
}
break;
case 'equal':
$singleKey = explode(',', $key);
if(empty($_POST[$singleKey[0]]))
{
$errs[] = $names[$singleKey[0]];
}
elseif(empty($_POST[$singleKey[1]]))
{
$errs[] = $names[$singleKey[1]];
}
else
{
if($_POST[$singleKey[0]] != $_POST[$singleKey[1]])
{
$errs[] = $names[$singleKey[0]].' & '.$names[$singleKey[1]].' komen niet overeen';
}
}
break;
default:
if(empty($_POST[$key]))
{
$errs[] = $names[$key];
}
}
}
return $errs;
}
?>
Daarnaast heb ik even een voorbeeld gemaakt hoe je de functie benaderd en hoe je de fouten laat zien.
Code (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
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
<?php
//In deze array geef je de naam op van de input veld (de key) en waarop hij moet controleren (de value). Momenteel controleerd hij op een valid email adres (email), numerieke waardes (numeric) en of het veld wel ingevuld is (value)
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$requiredFields = array('txtLoginUsername' => 'value','txtLoginPassword,txtLoginPasswordRepeat' => 'equal','txtLoginEmailAdress' => 'email');
$fieldNames = array('txtLoginUsername' => 'Login naam','txtLoginPassword' => 'login wachtwoord','txtLoginEmailAdress' => 'Login e-mail adres');
$checkFields = $this->checkinput->inputCheck($requiredFields, $fieldNames);
if(count($checkFields) < 1)
{
}
else
{
$this->data['msg'] = '<div class="errormsg">
U heeft volgende velden niet goed ingevuld.
<ul>';
foreach ($checkFields as $field)
{
$this->data['msg'] .= '<li>'.$field.'</li>';
}
$this->data['msg'] .= '</ul>
</div>';
}
}
?>
//In deze array geef je de naam op van de input veld (de key) en waarop hij moet controleren (de value). Momenteel controleerd hij op een valid email adres (email), numerieke waardes (numeric) en of het veld wel ingevuld is (value)
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$requiredFields = array('txtLoginUsername' => 'value','txtLoginPassword,txtLoginPasswordRepeat' => 'equal','txtLoginEmailAdress' => 'email');
$fieldNames = array('txtLoginUsername' => 'Login naam','txtLoginPassword' => 'login wachtwoord','txtLoginEmailAdress' => 'Login e-mail adres');
$checkFields = $this->checkinput->inputCheck($requiredFields, $fieldNames);
if(count($checkFields) < 1)
{
}
else
{
$this->data['msg'] = '<div class="errormsg">
U heeft volgende velden niet goed ingevuld.
<ul>';
foreach ($checkFields as $field)
{
$this->data['msg'] .= '<li>'.$field.'</li>';
}
$this->data['msg'] .= '</ul>
</div>';
}
}
?>