0

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

3
Contributors
10
Replies
12
Views
5 Years
Discussion Span
Last Post by Squidge
Featured Replies
  • 1

    > 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 … Read More

0

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

0

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 @?

0

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.

0

@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";
    }
}

Edited by Squidge

1

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?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.