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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:43:44+00:00 2026-05-31T03:43:44+00:00

I have several variables and I would like to validate them before writing them

  • 0

I have several variables and I would like to validate them before writing them into the mysql database. Basically I would like the following logic: if the username is ok, and the email is ok and the password is ok then write all to the db, else spit out an error for each.

I have tried to come up with the following logic but there must be a more logical and efficient way to do it (sorry, I know this looks primitive):

if ( preg_match("/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/", $username) ) {
    if ((preg_match("^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$", $email)) {
        if (/*password is ok*/) {
            //create user entry in the db
            //and save success message in the session variable
        } else {
            //error message for password
        }
    } else {
        //error message for email
    }
} else {
    //error message for username
}
  • 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-05-31T03:43:46+00:00Added an answer on May 31, 2026 at 3:43 am

    First of all, there are lots of frameworks and validation scripts that allow you to do this. On the other hand, it’s good to have done this yourself, so you know what is actually going on, should you later decide to switch to a framework.

    Your code looks decent, but from a usability point-of-view, it isn’t as good as one could have hoped. If I try to create a user in your system, I might have to fill out the forms and press submit several times, which can be quite annoying. Instead, once the form has been filled out, a list of errors should show up, instead of just one error at a time. A common way to handle this problem is by creating an array of error messages.

    An implementation could look something like this

    $errors = array();
    
    if (!preg_match("/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/", $username))
        array_push($errors, "Invalid username");
    
    if (!preg_match("^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$", $email))
        array_push($errors, "Invalid password");
    
    if (otherValidationRule())
        array_push($errors, "Something else went wrong");
    

    After this, you check whether any errors occured and print them if so

    if (sizeof($errors) > 0) {
        print("<ul>\n");
        foreach($errors as $error)
            printf("<li>%s</li>\n", $error);
        print("</ul>\n");
    }
    else {
        // Proceed with registration
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several variables in an SSIS package that I would like inserting into
I have several old 3.5in floppy disks that I would like to backup. My
On my web site I would like to have several sub-domains. Files that create
I have several variables like: class X(object): ... class XY(X): ... class XZ(X): ...
I would like to have your opinions regarding best practices to adopt in SQL
I have several tables/variables (sampled below): mytable = { ['100'] = { ['2']=0,['3']=0,['5']=0,['6']=0,['7']=0,['9']=0}, ['101']
I would like to put an int into a string . This is what
I'm quite confused about several books in .NET that I have read. Would someone
what I would like to do is to add several sequence numbers to one
I would like to join several times with the same table function for different

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.