1

I need to validate submitted form data:

  1. To check whether fields are empty.
  2. Proceed to validate non-empty data.

x – allows only spaces, underscore, aplha numeric characters. y – checks whether it is an image or not.

I'm using below code, it does not work. It is not validating and printing error.

<?php
$validate = array(
    '/^[a-z\d ]{4,20}$/i' => array('$x' => 'Please enter valid name.'),
    '/^[a-z\d ]{4,20}$/i' => array('$y' => 'Please enter a real category.')
);

$error = '';

foreach ($validate as $key => $field)
{
    if (preg_match($key,$field[0]))
    {
       $error.= $field[0];
    }
}

if ($error)
{
    echo $error;
    exit;
}
3
  • Where does the variable $validate come from? I guess it should be $regEx? Commented Jun 19, 2012 at 8:36
  • Define it does not work. Does the validation pass when it should fail, does it fail when it should pass? Commented Jun 19, 2012 at 8:38
  • @MrCode:it is not validating and printing error. Commented Jun 19, 2012 at 8:39

2 Answers 2

1

You're matching your regex against your error message, not your submitted strings ($x and $y).

You probably ment to do something like this:

$validate = array(
    '/^[a-z\d ]{4,20}$/i' => array($x, "Please enter valid name."),  //change here
    '/^[a-z\d ]{4,20}$/i' => array($y, "Please enter a real category.") //change here
);

$error = '';
foreach ($validate as $key => $field)
{
    if (preg_match($key,$field[0]))
    {
       $error.= $field[1]; //change here
    }
}

if($error)
{
    echo $error;
    exit;
}

UPDATE

how I would do it.

$validate = array(
    array($x, '/^[a-z\d ]{4,20}$/i', "Please enter valid name."),
    array($y, '/^[a-z\d ]{4,20}$/i', "Please enter a real category."),
    array($phone, '/^\(?[0-9]{3}\)?|[0-9]{3}[-. ]? [0-9]{3}[-. ]?[0-9]{4}$/' , "Please enter a valid phone number")
);

$error = '';
foreach ($validate as $validation)
{
    if (!preg_match($validation[1],$validation[0]))
    {
       $error .= $validation[2]; 
    }
}

if($error != '')
{
    echo $error;
    exit;
}

$validate now is an array of your fields, each expressed with it's own array containing error message, regex to match it against and submitted subject.

Sign up to request clarification or add additional context in comments.

8 Comments

:Did you try it out in local host,if the user enter less than 4 or special characters,it should print the error message.Both x and y are two different field data so each have diff regex.Your code does not give me any error,if i enter characters less than 4.
:There can be nfeilds and n regex.I can n't write field[0],field[1].
I corrected only the obvious part about matching the regex against the error message. I didn't touch your validation logic. Indeed, You're printing the error message only when the string matches, which, I presume, means that it is valid. I'll append to my answer how I would do it.
:Will it validate empty feilds.Say,if'i've to validate 10 fields.How do i change your code.
empty fields don't match your regex, so it will treat that as an error. To add a field, you have to add an array to $validate, just like the 2 that are already there.
|
1

Use filter_var functions. You have the options you need there.

2 Comments

:I'm using php 4,it is only supported for PHP 5.
I don't see ANY reason to use php4 in 2012, even for legacy code. PHP5 was released in 2005. PHP4 is not supported and a security risk.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.