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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:26:55+00:00 2026-05-15T19:26:55+00:00

I want a rails model to exclude certain patterns: runs of two or more

  • 0

I want a rails model to exclude certain patterns: runs of two or more spaces.

User.name = "Harry Junior Potter" is valid, but User.name = "Harry Junior Potter" is not (two spaces between Harry and Junior).
This to avoid identity theft, where those two names are displayed the same (HTML compresses runs of whitespace).

In other words: Allowed is: [0-9A-z_-] and ‘\s only in series of one’.

My regular-expression is too poor to craft such a regexp, this is what I have (with a negative lookahead, but it does not match correctly.

 /([0-9A-z_\-])(\s(?!\s))?/

Note: a before_validation hook already strip()s all the elements, so spaces at begin or end of the string are not a problem.

  • 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-15T19:26:56+00:00Added an answer on May 15, 2026 at 7:26 pm

    First off, [A-z] is an error. It’s equivalent to

    [A-Z\[\\\]^_`a-z]
    

    …and I’m pretty sure that’s not what you had in mind. You have to spell out the two ranges separately: [A-Za-z]. But in this case you’re also matching digits and underscore, so you can use \w like @Sjuul did: [\w-]+. That makes your regex

    /^[\w-]+(?: [\w-])*$/
    

    Of course, that will match silly things like -- - ---, and it won’t match a lot of real names. I’m just answering your question about allowing only a single space between names.

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

Sidebar

Related Questions

What is the more rails-like? If I want to modify a model's property when
I have a model in rails, lets say User , which i want to
In my rails 3 model, I have two classes: Product, Service. I want both
I want to create a rails' model like FileURL without having to name the
I want to use tags for a model in my Rails application, and I'd
Can't figure this one out. In rails model, I want to call a method
I want to implement authorization in my Rails application on a model level (not
I'm building Rails application and I want to have user registration/login functionality. I also
I'm creating a nested model in Rails, but I want to add fields to
I've got a User model in my Rails 3 app which is getting huge.

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.