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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:22:14+00:00 2026-05-23T02:22:14+00:00

i need to write a case which only except the a-zA-Z0-9 characters with underscore

  • 0

i need to write a case which only except the a-zA-Z0-9 characters with underscore and white space(1 or more than 1) and ignore all rest of the characters.I wrote a code but its not working properly.
In those case should be wrong but its show OK

1) test msg@ 
2) test@msg  
3) test!msg

also those should be OK but currently shows wrong.

1) test msg.-(Two white space)

what i should to change in my code .pls help and see my code below.

$message=$_GET['msg'];

if(preg_match('/[^A-Za-z0-9]\W/',$message))
{
   echo "Wrong";
}
else
{
 echo "OK";
}
  • 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-23T02:22:15+00:00Added an answer on May 23, 2026 at 2:22 am

    Here’s an optimized version of the one left by riad:

    $message = $_GET['msg'];
    
    if ( preg_match('/^[a-z0-9_ ]+$/i', $message) )
    {
       echo 'Ok';
    }
    else
    {
     echo 'Wrong';
    }
    

    I’ve removed the A-Z (uppercase) from the regular expression since the i modifier is used.

    I’d also like to explain what you did wrong in the example you provided.

    First, by putting the ^ inside the square brackets ([]), you’re essentially doing the opposite of what you were trying to do. Place a ^ inside the square brackets means "not including."

    You were missing a *, + or ? at the end of the square bracket, unless you only wanted to match a single character. The * character means 0 or more, + means 1 or more and ? means 0 or 1.

    The \W means any non-word character. That’s probably not what you wanted.

    Finally, to starting a regular expression with ^ means that the beginning of the string you’re string to match must start with whatever is after the ^. Ending the regular expression with a $ means that the string must end with the characters preceding the $.

    So by typing /^[a-z0-9_ ]+$/i you’re saying match a string that starts with a-z0-9_ or a space, that contains at least of those characters (+) and ends.

    PHP has a lot of documentation of the PCRE regular syntax which you can find here: https://www.php.net/manual/en/reference.pcre.pattern.syntax.php.

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

Sidebar

Related Questions

i'm need to write a function that will flip all the characters of a
I need to delete an application (MyApp.app), which has read only permissions in all
I need write an update statement that used multiple tables to determine which rows
string sqlQuery = unknown; I need to write a function which receives a sql
I need to write a program used internally where different users will have different
I need to write a Java Comparator class that compares Strings, however with one
I need to write a Delphi application that pulls entries up from various tables
I need to write a web application using SQL Server 2005, asp.net, and ado.net.
I need to write a java script. This is supposed to validate if the
I need to write code that picks up PGP-encrypted files from an FTP location

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.