oo00oo
2006-Jan-20 18:52 UTC
[Rails] validates_format_of > Invalid regular expression with simple pattern
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 ?
Ezra Zygmuntowicz
2006-Jan-20 21:11 UTC
[Rails] validates_format_of > Invalid regular expression with simple pattern
On Jan 20, 2006, at 10:52 AM, oo00oo wrote:> 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 ? > > > > > _______________________________________________ > 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
Rob Biedenharn
2006-Jan-20 21:51 UTC
[Rails] validates_format_of > Invalid regular expression with simple pattern
At 1/20/2006 01:52 PM, you wrote:>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 ?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
Bob Silva
2006-Jan-21 01:53 UTC
[Rails] Re: validates_format_of > Invalid regular expression with si
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-- Posted via http://www.ruby-forum.com/.
oo00oo
2006-Jan-21 09:53 UTC
[Rails] Re: validates_format_of > Invalid regular expression with si
Oh yes from "." to " ", I understand my mistake. I also use + instead * Thanks to all ! :)> 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 >