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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:02:17+00:00 2026-06-16T01:02:17+00:00

I am trying to validate a string, that should contain letters numbers and special

  • 0

I am trying to validate a string, that should contain letters numbers and special characters &-._ only. For that I tried with a regular expression.

var pattern = /[a-zA-Z0-9&_\.-]/
var qry = 'abc&*';
if(qry.match(pattern)) {
    alert('valid');
}
else{
    alert('invalid');
}

While using the above code, the string abc&* is valid. But my requirement is to show this invalid. ie Whenever a character other than a letter, a number or special characters &-._ comes, the string should evaluate as invalid. How can I do that with a regex?

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

    Add them to the allowed characters, but you’ll need to escape some of them, such as -]/\

    var pattern = /^[a-zA-Z0-9!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]*$/
    

    That way you can remove any individual character you want to disallow.

    Also, you want to include the start and end of string placemarkers ^ and $

    Update:

    As elclanrs understood (and the rest of us didn’t, initially), the only special characters needing to be allowed in the pattern are &-._

    /^[\w&.\-]+$/
    

    [\w] is the same as [a-zA-Z0-9_]

    Though the dash doesn’t need escaping when it’s at the start or end of the list, I prefer to do it in case other characters are added. Additionally, the + means you need at least one of the listed characters. If zero is ok (ie an empty value), then replace it with a * instead:

    /^[\w&.\-]*$/
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've a regular expression that should validate if a string is composed by space-delimited
I have a CSV string that I am trying to validate via regex to
I'm trying to validate a string in an xsd that requires the use of
I’m trying to validate a string which contains numbers where each four numbers are
I am trying to validate a string as sha1 with preg_match() and seem to
I'm trying to validate the input of multiple text boxes (i.e. they should be
I've been trying to validate an inputted string ( sys argv[1] in this case).
I'm trying to write a xml schema that will validate this piece of xml:
I'm trying to validate an XML file against an XSD using the function schemaValidate(String
I have an Address object that I am trying to validate data against using

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.