Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8105441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:09:53+00:00 2026-06-06T00:09:53+00:00

I’ve five fields in the form When a form is submitted and if three

  • 0

I’ve five fields in the form

When a form is submitted and if three fields are not filled,it should validate and show errors on those three fields.

I used to do it using if loops,this will show one error at a time,instead, I want to show all the errors.

I want to check special characters,empty validation,min and max characters on each field.

      preg_match('/^[a-z\d ]{3,20}$/i', $category

How to check them all at once using PHP?

Update

 $errors = array();
 $required = array("Name", "Email");  
  foreach($_POST as $key=>$value)
   {
     if(!empty($value))
       {
           $$key = $value;
      }
     else
      {
     if(in_array($key, $required))
     {
         array_push($errors, $key);
     }
    }        

   }

This can be used to check empty validation for all fiedls,how do i check for special characters,alpha numeric characters,the provblem would be each field will have different regex.
For eg: phone number and email can not have same regex.

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T00:09:55+00:00Added an answer on June 6, 2026 at 12:09 am
    $errors = array();
    if (preg_match('/^[a-z\d ]{3,20}$/i', $name)) {
        $errors[] = "Please enter valid name.";
    }
    
    if (preg_match('/^[a-z\d ]{3,20}$/i', $category)) {
        $errors[] = "Please enter valid category.";
    }
    
    if (preg_match('/^[a-z\d ]{3,20}$/i', $amount)) {
        $errors[] = "Please enter valid amount.";
    }
    
    if(!empty($errors))
    {
        echo "The was an error filling out the form:<br><ul>";
        foreach($errors as $msg)
        {
            echo "<li>$msg</li>";
        }
        echo "</ul>Please try again.";
    }
    

    Or, to make it more concise, use something like Ananth’s answer.

    // your 3 fields that need to be validated; Key is field, Value is error message if check invalid.
    $validate = array("name" => "Please enter valid name.", "category" => "Please enter a real category.", "amount" => "Please enter an amount.");
    
    $error = array();
    foreach ($validate as $field => $message) {
        if (preg_match('/^[a-z\d ]{3,20}$/i', $$field)) {
            $error[] = $message;
        }
    }
    
    if(!empty($errors))
    {
        echo "The was an error filling out the form:<br><ul>";
        foreach($errors as $msg)
        {
            echo "<li>$msg</li>";
        }
        echo "</ul>Please try again.";
    }
    

    UPDATE

    Seeing as how each check has it’s on regex, the first example is easy enough to solve. As for the second example, it only requires a small change.

    // your 3 fields that need to be validated; 
    // Key is Regex, value is array with the variable name (without the $) as the key, 
    // and the error message as the value.
    
    $validate = array(
        '/^[a-z\d ]{3,20}$/i' => array("name" => "Please enter valid name."), 
        '/^[a-z\d ]{3,20}$/i' => array("category" => "Please enter a real category."),
        '/^[a-z\d ]{3,20}$/i' => array("amount" => "Please enter an amount.")
    );
    
    $error = array(); // Empty array to store errors in.
    
    
    foreach ($validate as $regex => $data) {  // Key = $regex, Value = array($variable, $error)
        if (preg_match($regex, ${$data[0]})) { // This code is untested, so try it without the braces around $data[0]
            $error[] = $data[1]; // If the data matches the regular expression provided, add the provided error message to the $error array.
        }
    }
    
    if(!empty($errors)) // If the $errors array isn't empty (has an error in it)
    {
        echo "The was an error filling out the form:<br><ul>";
        foreach($errors as $msg) // Goes through each error, printing it to an unordered list.
        {
            echo "<li>$msg</li>";
        }
        echo "</ul>Please try again.";
    }
    

    Note than in the above example, each has the same example regex, but it’s simple enough to change those to your needs.

    EDIT
    All the code above is untested, though it should work, if it doesn’t, try removing the braces around $data[0], as mentioned in the accompanying comments.

    UPDATE 2
    If you need to add an optional checker, the same code can be modified slightly, with an extra foreach loop, for all the optional fields to check.

    // your 3 fields that need to be validated; 
    // Key is Regex, value is array with the variable name (without the $) as the key, 
    // and the error message as the value.
    
    $required = array(
        '/^[a-z\d ]{3,20}$/i' => array("name" => "Please enter valid name."), 
        '/^[a-z\d ]{3,20}$/i' => array("category" => "Please enter a real category."),
        '/^[a-z\d ]{3,20}$/i' => array("amount" => "Please enter an amount.")
    );
    
    $optional = array(
        '/^[a-z\d ]{3,20}$/i' => array("shipping" => "Please enter valid shipping location."), 
        '/^[a-z\d ]{3,20}$/i' => array("options" => "Please enter an clean option.")
    );
    
    $error = array(); // Empty array to store errors in.
    
    
    foreach ($required as $regex => $data) {  // Key = $regex, Value = array($variable, $error)
        if (preg_match($regex, $$data[0])) { // This code is untested, so try it with or without the braces around $data[0]
            $error[] = $data[1]; // If the data matches the regular expression provided, add the provided error message to the $error array.
        }
    }
    
    foreach($optional as $regex => $data)
    {
        if(strlen(trim($$data[0])) > 0) // If the trimmed length of the string (all leading and trailing whitespace removed) == 0?
        {
            if (preg_match($regex, $$data[0])) { // This code is untested, so try it with or without the braces around $data[0]
                $error[] = $data[1]; // If the data matches the regular expression provided, add the provided error message to the $error array.
        }
    }
    
    if(!empty($errors)) // If the $errors array isn't empty (has an error in it)
    {
        echo "The was an error filling out the form:<br><ul>";
        foreach($errors as $msg) // Goes through each error, printing it to an unordered list.
        {
            echo "<li>$msg</li>";
        }
        echo "</ul>Please try again.";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.