Regular Expressions

Hi there

This might be a stupid question, but I didn’t found anything googling
the 'Net.

I have created a “stupid” little scaffold to insert data into the db.
but i want to validate the data…

This works perfectly:

[SNIP]
class Phrase < ActiveRecord::Base
validates_presence_of :r_or_s, :number, :description
validates_numericality_of :number
end
[/SNIP]

but now, I want to check if the r_or_s-field-value is really “r” or “s”.

so I heard about regular expressions and tried doing that:
validates_format_of :r_or_s, :with => ^(r|s)$, :on => :create

Why doesn’t that work? Where is a reference who shows me how to use
regular expressions???

kindest regards

markus

validates_format_of :r_or_s, :with => ^(r|s)$, :on => :create

Why doesn’t that work? Where is a reference who shows me how to use
regular expressions???

You could try:

validates_format_of :r_or_s, :with => /^(r|s)$/, :on => :create

For reference checkout http://www.regular-expressions.info/ or the more
in
depth O’Reilly book “Mastering Regular Expressions, Powerful Techniques
for Perl and Other Tools”.


Company - http://primalgrasp.com
Thoughts - http://deezsombor.blogspot.com

Markus Z. wrote:

so I heard about regular expressions and tried doing that:
validates_format_of :r_or_s, :with => ^(r|s)$, :on => :create

You can do this without regular expressions

validates_inclusion_of :r_or_s, :in => %w( r s ), :on => :create

On 22/08/06, Markus Z. [email protected] wrote:

validates_format_of :r_or_s, :with => ^(r|s)$, :on => :create

validates_format_of :r_or_s :with => /^(r|s)$/, :on => :create

Cheers,
Hasan D. [email protected]