pruett
pruett

Reputation: 2121

Ruby Format Validations [ No Whitespace ]

I'm trying to create a format validation for a text field that will reject anything with whitespace. Can someone help me out with the RegEx syntax? This is what I've tried:

no_whitespace = /\A[\S]\z/i

validates :customurl,  :format => { :with => no_whitespace }

I'm new to programming and clueless about RegEx. Any help would be greatly appreciated. Thanks!

Upvotes: 0

Views: 2240

Answers (2)

Dave Isaacs
Dave Isaacs

Reputation: 4539

Try this:

no_whitespace = /^[\S]+$/

That should specify no whitespace characters from the beginning (^) the the end ($) of the string, and at least 1 character.

Upvotes: 4

Eric Yang
Eric Yang

Reputation: 1889

Try this:

no_whitespace = /[\S]*/

Use Rubular to help you form and test regular expressions.

Upvotes: 2

Related Questions