Forum: Ruby on Rails validates_format_of > Invalid regular expression with simple

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
19403f509ca90e8ffe072214c6cba896?d=identicon&s=25 oo00oo (Guest)
on 2006-01-20 20:30
(Received via mailing list)
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 ?
6076c22b65b36f5d75c30bdcfb2fda85?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-01-20 22:49
(Received via mailing list)
On Jan 20, 2006, at 10:52 AM, oo00oo wrote:

> The pattern is really simple but I have this error :
>
>
>
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>

Try this regex instead:

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

Cheers-

-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra@yakima-herald.com
509-577-7732
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2006-01-20 23:29
(Received via mailing list)
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
C71c78dabb4acc92c9b17803cb9429fe?d=identicon&s=25 Bob Silva (Guest)
on 2006-01-21 03:29
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 Biedenharn 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
19403f509ca90e8ffe072214c6cba896?d=identicon&s=25 oo00oo (Guest)
on 2006-01-21 11:33
(Received via mailing list)
Oh yes from "." to " ", I understand my mistake.
I also use + instead *
Thanks to all !
:)
This topic is locked and can not be replied to.