Validates_format_of > Invalid regular expression with simple


#1

Hello,

I try this :

validates_format_of :name , :with => /^[A-z0-9_.- ]*$/ , :message =>
“bad characters”

for accept any name with chars “A” to “z” , “0” to “9” , with “_” “.”
“-” and " "

The pattern is really simple but I have this error :

SyntaxError in Login#register

./script/…/config/…/app/models/user.rb:6: invalid regular expression:
/^[A-z0-9_.- ]*$/

This is strange. I forgot something ?


#2

On Jan 20, 2006, at 10:52 AM, oo00oo wrote:

The pattern is really simple but I have this error :


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

Try this regex instead:

/^[a-zA-Z0-9_.-]*$/

Cheers-

-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
removed_email_address@domain.invalid
509-577-7732


#3

At 1/20/2006 01:52 PM, you wrote:

The pattern is really simple but I have this error :

SyntaxError in Login#register

./script/…/config/…/app/models/user.rb:6: invalid regular
expression: /^[A-z0-9_.- ]*$/

This is strange. I forgot something ?

You can’t have [.- ] because that looks like the range from ‘.’ to ’
’ and space is lower than full-stop.

Try: /^[A-z0-9 _.-]$/ or (as someone suggested: /^[A-Za-z0-9
_.-]
$/ if you didn’t intend [ (0x5B), \ (0x5C), ] (0x5D), ^ (0x5E),
_ (0x5F), and ` (0x60) to be included between Z and a)

-Rob


#4

In other words, if you want your regular expression to match a hypen, it
must be the first or the last character within the []:

/^[A-z0-9_. -]$/
/^[-A-z0-9_. ]
$/

Also, are you sure you want the * quantifier, as this will also match
the empty string. Using + may be what you are looking for in this case.

/^[-A-z0-9_. ]+$/

Cheers,

Bob

Rob B. wrote:

At 1/20/2006 01:52 PM, you wrote:

The pattern is really simple but I have this error :

SyntaxError in Login#register

./script/…/config/…/app/models/user.rb:6: invalid regular
expression: /^[A-z0-9_.- ]*$/

This is strange. I forgot something ?

You can’t have [.- ] because that looks like the range from ‘.’ to ’
’ and space is lower than full-stop.

Try: /^[A-z0-9 _.-]$/ or (as someone suggested: /^[A-Za-z0-9
_.-]
$/ if you didn’t intend [ (0x5B), \ (0x5C), ] (0x5D), ^ (0x5E),
_ (0x5F), and ` (0x60) to be included between Z and a)

-Rob


#5

Oh yes from “.” to " ", I understand my mistake.
I also use + instead *
Thanks to all !
:slight_smile: