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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:13:58+00:00 2026-05-25T13:13:58+00:00

Im creating a search function. The users are only allowed to use a-z A-Z

  • 0

Im creating a search function. The users are only allowed to use a-z A-Z and 0-9. How do i check if $var only contains text or numbers, and not special characters ?

I have tried something like this:

if (!preg_match('/[^a-z]/i', $search) {
$error = "error...";
}

If anyone have a smarter solution, please let me know. It could also be something checking for special characters.

  • 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-25T13:13:59+00:00Added an answer on May 25, 2026 at 1:13 pm

    You’re pretty much there. Just add numbers 0-9 to your regular expression, like this:

    if(preg_match('/[^a-z0-9]/i', $search)) 
    {
        $error = "Error...";
    }
    

    The /i flag tells the expression to ignore case, so A-Z is not needed inside the letter list.

    In your original code, you were looking for anything that wasn’t a letter or number, while also checking to see if preg_match() hadn’t matched anything – you created a double negative. The code above executes the if() if anything that isn’t a letter or number is found. Full credit to @brain in the comments.


    To allow other characters, simply add them to the characters inside the braces:

    if(preg_match('/[^a-z0-9 \.]/i', $search)) 
    {
        $error = "Error...";
    }
    

    This example allows spaces and . (dots).

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

Sidebar

Related Questions

I'm doing this system Stacked and I am creating the search function. And in
I'm creating eBay style search panes that allow users to narrow their results set
I'm creating a set of search panes that allow users to tweak their results
I'm creating a search function for my website where the user can put in
I am creating a small search function on my site, this enables the search
I am creating a search function of my classifieds on my website. Here is
I'm creating a page search function and I need to compare the contents of
I am creating auto suggest function in my website search box, every time the
I'm creating a web page that search users in the database. When I'm typing
I'm currently creating a search function in lua which basically just goes through a

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.