I think everyone who has ever programmed, know how dreary and boring it is often to write algorithms to validate input data. I want to offer you one solution that will help you build your own base, which can be used from project to project, changing and supplementing your needs.
An example is given in PHP, but I am sure you can easily translate it into any other language.
The idea is to use the
switch statement . In short, this operator allows you to compare the value of a variable and execute blocks of code depending on the results of the comparison. For example, instead of this:
<? php
if ($ i == 0)
{
echo "i is 0";
}
elseif ($ i == 1)
{
echo "i is 1";
}
elseif ($ i == 2)
{
echo "i is 2";
}
?>
You can write:
<? php
switch ($ i)
{
case 0:
echo "i is 0";
break;
case 1:
echo "i is 1";
break;
case 2:
echo "i is 2";
break;
}
?>
Now imagine that we have an array of data $ a containing a string with a Name (index name) and an integer Age (index age). This data is entered by the user, so we need to check them.
To check we have the following class:
<? php
class naughty_checks
{
var $ data = array ();
/ * Class constructor, array of input data to check * /
function naughty_checks ($ data)
{
$ this-> data = $ data;
}
/ * Check for field completion * /
function field_empty ($ field)
{
if (! isset ($ this-> data [$ field]) || strlen (trim ($ this-> data [$ field])) == 0)
{
return true;
}
else
{
return false;
}
}
/ * Check if a field is a positive integer * /
function field_numeric ($ field)
{
if (! isset ($ this-> data [$ field]) || intval (trim ($ this-> data [$ field])) <1)
{
return true;
}
else
{
return false;
}
}
}
?>
This is the class that you will expand with your own checks.
And now the main salt. I will give the check in the form of a usual code, I think then to put it into a separate method and screw the output of the error message and other delights you can do yourself.
<? php
// $ a - our array
$ error_flag = false;
$ error_text = ''; // Error text
$ _chck = new naughty_checks ($ a); // Initialize the class, pass our array
/ * Magic * /
<b> switch (true)
{
case ($ _ chck-> field_empty ('name')):
$ error_flag = true;
$ error_text = 'You have not filled the Name field.';
break;
case ($ _ chck-> field_numeric ('age')):
$ error_flag = true;
$ error_text = 'You did not fill in, or you incorrectly filled in the Age field.';
break;
} </ b>
if ($ error_flag)
{
die ($ error_text);
}
else
{
echo 'Have you ever seen a UFO?';
}
?>
')
I hope you got the gist. Such switches can be combined into methods and used to validate certain data. This approach will make your code more readable than if you use multiple if statements.
This method allows you to avoid unnecessary checks, because as soon as at least one testing method returns true, no further checks will be performed.
Here is such a not very ambitious, but still interesting, from my point of view, approach to checking the entered data. I sincerely hope that at least someone will be useful. Also, I am pleased, but I will answer all your questions in the comments to the best of my ability.
Actually, it was my thoughts out loud.
I hope you also have thoughts, and you are not afraid to express them out loud ...