Hi all,

I am working on a generic form validation class.

However I have hit a stubling block. Form fields will never be the same from user to user.

Is there a work around for this? Would it be easier to use numerical values for field names and use multiple validation functions (minimal duplication then :))?

Any assistance would be greatly appreciated

Can you share some code, so it's easier to understand what you have done?

its all very messy at the moment, so would prefer not to post as yet.

But because form fields are always different this is where i cannot get my head round how to set for this in the class

Define in an array the fieldname and the validation it requires. Easy enough to use and change.

ok i have tidied a small part:

foreach ($_POST as $formIdentifier => $userInput)
{

    // Filter
    if(strtolower($formIdentifier) == "email")
    {
        if(!filter_var($userInput, FILTER_VALIDATE_EMAIL))
        {
            echo "email is invalid";
        }
    }
}

How can i change the "email" to check the variable for any @?

In response to your question, you could use the strpos function.

However, why not just validate that it's an actual email address? Why do you care if the string contains a @ symbol, if it isn't an email address? It still wouldn't be valid.

Have you thought about looking at how PHP frameworks incorporate validation?

What you're trying to do sounds very similar to Zend Framework.
http://framework.zend.com/manual/en/zend.validate.html

Zend Validators work on the basis that each validator class has an isValid method, that takes an input value and returns true or false. You then chain validators together to create more complex validation rules.

E.g. you'd have one class for validating the length of a string, accepting the string, min and max values. Another for validating the the string is a valid email address. Etc.

@blocblue,

The idea would be to idetify the form input that contained @, which would then be indentified as an email address, which i can then validate using the FILTER_VALIDATE_EMAIL.

Hope that makes sense.

:EDIT:

So i could in theory then use just this (?):

foreach ($_POST as $formIdentifier => $userInput)
{
    // Filter
    if(!filter_var($userInput, FILTER_VALIDATE_EMAIL))
    {
        echo "email is invalid"; 
        // Add into badData[] array
        # $badData[] ="You have entered an invalid email address";
    }
}

Can anyone else help out on this?

The idea would be to idetify the form input that contained @, which would then be indentified as an email address, which i can then validate using the FILTER_VALIDATE_EMAIL.

foreach ($_POST as $formIdentifier => $userInput)
{
    // Filter
    if((strpos($userInput, '@') !== false) and !filter_var($userInput, FILTER_VALIDATE_EMAIL))
    {
        echo "email is invalid"; 
        // Add into badData[] array
        # $badData[] ="You have entered an invalid email address";
    }
}

Is that what you are after?

@pritaeas, many thanks

+1