Take the 2-minute tour ×

I need a regular expression for my Ruby on Rails application for the password field.

Any character or number or symbols is allowed except space.

share|improve this question

closed as too broad by Cupcake, gnat, ArtB, Daniel Lisik, Shankar Damodaran Jun 1 at 1:37


Out of interest, why would you not allow space? –  Dan Puzey Jul 16 '10 at 14:35
is it advisable to allow spaces in password? –  mcxiand Jul 25 '11 at 3:54
@mcxiand it's not uncommon to allow spaces in a password. Windows, for example, accepts spaces in passwords. By allowing spaces, you increase the total number of possible passwords that users can use, thus helping make the passwords harder to brute force, thus making the passwords more secure. There's little to no reason not to allow spaces in passwords. –  Cupcake May 31 at 18:28
Is this validation happening client-side (Javascript), or server-side (Ruby)? I think these recent edits are incorrect and the use of Rails is incidental. I also disagree with the question being closed as too broad; it is a pretty specific requirement. –  Jay Jun 1 at 20:39

3 Answers 3

up vote 8 down vote accepted

If this is client-side validation in Javascript (or any language other than Ruby), this expression will match a string with no whitespace (\S) at least one character (+), no max:


Ruby is the only language that uses multi-line mode by default, so the start-of-line ^ and end-of-line $ behave differently (they match once per input, no matter how many lines). So, if you are validating the input in Ruby, you'd need to use \A for start-of-line and \Z for end-of-line.


share|improve this answer
Your regex doesn't do what you claim it does. What specifically does ^ mean in a Ruby regex? How about $? –  mu is too short May 31 at 18:41
@muistooshort The ruby and ruby-on-rails tags were added yesterday. I think the validation is actually being done client-side, so my answer is for Javascript (or any other language except Ruby). Ruby is the only language that uses multi-line mode by default, so you can replace ^ with \A and $ with \Z. –  Jay Jun 1 at 20:31

All except spaces, do you need to narrow the results a bit more than this?

/[^ ]+/
share|improve this answer
User could paste in a tab character. I know the OP didn't say "whitespace," but I think that's the intent. –  Jay Jul 16 '10 at 14:38

This is without minimum length (or rather, with minimum length 1):


With minimum length 8:


or, if your regex engine supports it (don't know why it wouldn't):

share|improve this answer
The first one has a minimum length of 1 and these ignore the requirement to exclude spaces. –  Jay Jul 16 '10 at 14:36
@Jay: Yeah, I fixed the first while you were commenting and just fixed the second. Got . mixed up with \w/\S for a moment. –  JAB Jul 16 '10 at 14:37
Okay, but \w still doesn't fit the bill, I'm afraid. OP asks for "any character or numbers or symbols" which would include punctuation. \w is only alpha, numeric, and underscore. –  Jay Jul 16 '10 at 14:40
@Jay: Again, corrected that while you were commenting. –  JAB Jul 16 '10 at 14:42
^ does not mean beginning of string in Ruby regexes, $ does not mean end of string. –  mu is too short May 31 at 18:44
