I am using HTML/PHP to read data entered by the user, i only want the user to be able to enter numbers,and periods the form, if not an error is thrown.
Because the form is dynamic, the user can add and delete form fields, so i need to run the pregmatch against all of the POST variables. Here is my script:
array_walk_recursive($_POST, function ($v) {
if (preg_match('/^[a-zA-Z.\d]+$/', $v)) {
} else {
exit("Error: Special characters and spaces not allowed.");
}
});
Here is an example of the POST that fails:
variable=HomeWin&fixtures=fixtures&option[]=1&number[]=2&option[]=11&number[]=&option4[]=1&away_number[]=2
Response:
Error: Special characters and spaces not allowed.
It fails because of the blank number variable:
&number[]=
When i add a number to the end like this it works:
&number[]=1
Full working POST data:
variable=HomeWin&fixtures=fixtures&option[]=1&number[]=2&option[]=11&number[]=1&option4[]=1&away_number[]=2
I suspect it may be failing because when the variable is blank it treats the & as the user entered data.
How can i modify my regex so that it accepts blank variables?