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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:19:11+00:00 2026-06-09T23:19:11+00:00

I have a working regex for when a user can enter only 1 word

  • 0

I have a working regex for when a user can enter only 1 word into a text box.
However, how can I adjust it, to allow 2,3,4,5 single words? At times, I may need to alter the code to only allow 3 words, for example that is.

Here is my regex ^([a-zA-Z_\-\.]+)$

  • 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-09T23:19:13+00:00Added an answer on June 9, 2026 at 11:19 pm

    For matching 2,3,4,5 single words

    ^(\b[a-zA-Z_\-\.]+?\b\s*){2,5}$
    

    For matching 2 or 4 single words

    ^(\b[a-zA-Z_\-\.]+?\b\s*){2}$|^(\b[a-zA-Z_\-\.]+?\b\s*){4}$
    or
    ^((\b[a-zA-Z_\-\.]+?\b\s*){2}|(\b[a-zA-Z_\-\.]+?\b\s*){4})$
    

    But it fails under tests abc.def and abc_def =( It founds two words. \b is the wrong idea with your vocabulary [a-zA-Z_\-\.]+…


    Okay, that’s it.

    For 2,3,4,5 words

    ^([a-zA-Z_\-\.]+[^a-zA-Z_\-\.]+){1,4}[a-zA-Z_\-\.]+$
    

    Notice that now non-alphabet symbols in the end are disallowed.

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

Sidebar

Related Questions

I have this regex working but now need to allow numbers without the decimal
I have a tag field in my web page, in which user can enter
I have working registration script the only problem is that i do not know
I am working on a project. This project have a user interface that we
Hello i have a TextField on my scene. It haves only digits, user input
I have the following two examples of html- <a href=http://foo.com>User</a>: <a style=color:#333 href=http://foo.com/word></a> blue
I have a basic CMS where a user can update a database of articles
I have id's validation method, each id can contain only [a-z0-9_] case insensitive characters.
I need help forming regex to limit user input to only numerics and only
I have a fck editor in which the user enters some text. And in

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.