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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:10:04+00:00 2026-06-19T01:10:04+00:00

I tried to look on here as to why this code is not working,

  • 0

I tried to look on here as to why this code is not working, got no where and now I’m hoping someone can help me out.

function validateData($string) {
    if (empty($string)) {
        return 'error';
    } elseif (strlen($string) <= 1) {
        return 'error';
    } elseif (preg_match('[a-zA-Z0-9]+\ ?', $string)) {
        return 'error';
    } else {
        return 'normal';
    }
}

When I execute the above code, using:

echo validateData('Test');
echo validateData('Test!');

These both echo ‘normal’.. however, the second example contains the ‘!’ in the string and should return ‘error’ because of the preg_match statement in the above code.

Achievement Objective. Check a string to make sure that it is not EMPTY, that it is longer than 1 character and only contains a-z, A-Z, 0-9 or a space. So no special characters.

Thank you very much in advance to all answers, I really appreciate it!

Ken

  • 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-19T01:10:05+00:00Added an answer on June 19, 2026 at 1:10 am

    If the string is empty, it will be equal to 0 when you ask for it’s string length, therefore testing empty($string) is useless since it is covered by the second test.

    Using a regex adds complexity without benefit here, there is a dedicated function to return true or false for alphanumeric string: ctype_alnum($string)

    Your function can just be:

    function validateData($string) {
        return (strlen($string) <= 1 || !ctype_alnum($string)) ? 'error' : 'normal';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This was working perfectly and now I'm not sure what happened. Here are the
I got this code for billboarding I created (With the help of others, and
(sorry but the code snippet is not working over here) <ul> <li><a href=# title=
I have tried to look everywhere for a solution but I can't seem to
I've tried to look for an answer to this, but none of the other
I have tried this example http://www.bennadel.com/blog/1056-ColdFusion-CFPOP-My-First-Look.htm , but it retrieve emails from POP server.
I tried to look after a similar problem but couldn't find any here. So
I was writing some code today and something was not working as I expected.
I do not have a specific block of code to post here, my website
I tried to look after a similar problem but couldn't find any here. So

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.