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 9008949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:04:13+00:00 2026-06-16T02:04:13+00:00

I am currently writing some PHP form validation (I have already validated clientside) and

  • 0

I am currently writing some PHP form validation (I have already validated clientside) and have some repetitive code that I think would work well in a nice little PHP function. However I am having trouble getting it to work. I’m sure it’s just a matter of syntax but I just can’t nail it down.

Any help appreciated.

//Validate phone number field to ensure 8 digits, no spaces.
if(0 === preg_match("/^[0-9]{8}$/",$_POST['Phone']) {
    $errors['Phone'] = "Incorrect format for 'Phone'";
}

if(!$errors) {
    //Do some stuff here....
}

I found that I was writing the validation code a lot and I could save some time and some lines of code by creating a function.

//Validate Function
function validate($regex,$index,$message) {
    if(0 === preg_match($regex,$_POST[$index])) {
        $errors[$index] = $message;
    }

And call it like so….

validate("/^[0-9]{8}$/","Phone","Incorrect format for Phone");

Can anyone see why this wouldn’t work?

Note I have disabled the client side validation while I work on this to try to trigger the error, so the value I am sending for ‘Phone’ is invalid.

  • 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-16T02:04:15+00:00Added an answer on June 16, 2026 at 2:04 am

    Let’s try something a little more thought out.

    You want to use this like so:

    if (validate(...)) {
        // It's ok
    }
    

    Then I’d suggest this:

    function validate($regex, $index, $message, &$errors) {     
        if (isset($_POST[$index]) && 1 === preg_match($regex, $_POST[$index])) {
            return true;            
        }
        $errors[$index] = $message; 
        return false;        
    }
    

    Now you have an opportunity to dump out of validation on error, or you can chain through these passing in the $errors and fill it with validation errors. No globals used.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing some c# code that is currently supposed to run as fast
I'm currently writing some php code to convert dates from the Gregorian Calendar to
I'm currently writing a java program that requires some Data to run. The data
I am currently re-writing an existing tool in PHP that another person started for
I am currently writing a program in Java that will accept strings from PHP
So I have a little problem with a PHP script I'm currently writing. To
I have a php script that displays a web form to the user. When
I am having some trouble with my PHP code. Basically I have a list
I'm currently writing a web application in HTML5/Javascript and PHP. Some employees of the
So I've got this PHP that's writing some JavaScript that looks something like this:

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.