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

  • Home
  • SEARCH
  • 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 8805895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:58:57+00:00 2026-06-14T01:58:57+00:00

Possible Duplicate: Regular Expression matching for entire string On my form page, I am

  • 0

Possible Duplicate:
Regular Expression matching for entire string

On my form page, I am trying to make it only accept alphanumeric characters for my username and password and require that they be from 6 to 15 characters. When I type in invalid data, it will insert it into the database rather than throw the user error that I defined in my CheckAlNum function.

functions.php

function checkAlNum($whichField) 
{
    if (preg_match('/[A-Za-z0-9]+/', $_POST[$whichField])){
        if ( (!count(strlen($whichField) >= 6)) OR (!count(strlen($whichField) <= 15 ))) {
            $message1 = '<p> Username and password must be between 6 and 15 characters </p>';
                return user_error($message1);
        }
        else{       
            return true;
        }           
    }

    else {
        $message = '<p>Username and password can only be numbers or letters</p>';
            return user_error($message);
     }


 } 

Form.php

        if (count($_POST) > 0) {

           //Validate the inputs
            $errorMessages = array();

            //Validate the username   
            $item5 = checkAlNum('username');
            if($item5 !== true) {
                $errorMessages[] = $item5;
            }

            //Validate the password
            $item6 = checkAlNum('password');
            if($item6 !== true) {
                $errorMessages[] = $item6;
            }

            //Validate the firstName and lastName
            $item1 = checkNameChars('firstName');
            if ($item1 !== true) {
                $errorMessages[] = $item1;

            }

            $item2 = checkNameChars('lastName');
            if ($item2 !== true) {
                $errorMessages[] = $item2;

            }   

            //Validate the office name
            $item3 = checkOfficeChars('office');
            if ($item3 !== true) {
                $errorMessages[] = $item3;

            }

            //Validate the phone number 
            $item4 = validate_phone_number('phoneNumber');
            if($item4 !== true) {
                $errorMessages[] = $item4;
            }  


            //Check to see if anything failed
            if (count($errorMessages) == 0) {

                $newEmployee = new Person;
                    $newEmployee -> insert();

            }

            else { //Else, reprint the form along with some error messages
                echo "<h2><span>Error</span>: </h2>";

                foreach($errorMessages as $msg) {
                    echo "<p>" . $msg . "</p>";
              }
            }
        }  

        ?>

I’ve tried playing around with the nesting of the if-else statements of the checkAlNum function and also the regex (although I’m pretty sure the regex is right). Maybe I’m just missing something really silly?

  • 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-14T01:58:58+00:00Added an answer on June 14, 2026 at 1:58 am
    function checkAlNum($whichField) 
    {
        if (preg_match('/^[a-z0-9]{6,15}$/i', $_POST[$whichField])) {
            return true;          
        }
        else {
            $message = '<p>Username and password can only be numbers or letters, 6-15 characters long</p>';
                return user_error($message);
         }
    }
    

    Without the ^ and $ anchors, your regex only checks whether there are alphanumerics anywhere in the field, not that the whole thing is alphanumeric. And changing + to {6,15} implements the length check here, so you can remove that extra check in your code.

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

Sidebar

Related Questions

Possible Duplicate: Regular Expression To Anglicize String Characters? What would be the best way
Possible Duplicate: Regular Expression to find a string included between two characters, while EXCLUDING
Possible Duplicate: Regular Expression Sanitize (PHP) I want to make my links SEO. Before
Possible Duplicate: Split a String based on regex I've never been a regular expression
Possible Duplicate: Regular expression, split string by capital letter but ignore TLA I have
Possible Duplicate: Regular expression for excluding special characters How would I check if a
Possible Duplicate: Matching Nested Structures With Regular Expressions in Python I am trying to
Possible Duplicate: matching unicode characters in python regular expressions Using re.findall(r'\w+', ip) on Fältskog
Possible Duplicate: R regular expression: http matching I'm working to capture URLs from a
Possible Duplicate: Regex to match URL I wrote a regular expression to validate only

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.