Why is this Rails regex matching this string? - ruby-on-rails

I'm using the following regex to validate one of my models
validates :login,
:format => {:with => /[A-Za-z][A-Za-z0-9_]+/}
And one of my tests is failing because this regex is matching this string, passing it as valid
a+df
What am I missing here?

It matches the df substring. Use anchors to force the regex to match the string in its entirety:
validates :login,
:format => {:with => /\A[A-Za-z][A-Za-z0-9_]+\Z/}

Try this regex
^[A-Za-z][A-Za-z0-9_]+$

Related

How to validate price in input?

I have an input (it's text input) where users can set up the price.
The problem is, that users can sometimes set up the price like this:
9
9.99
9,99
$9.99
What's the best way to validate this input? I am running the app on Heroku (= PostgreSQL database), the data type column is decimal.
Rails 4 complains about using $ and ^, so use:
validates :price, :presence => true,
:format => { :with => /\A(\$)?(\d+)(\.|,)?\d{0,2}?\z/ }
Just trying to save people time ;-)
Try something like below. It matches all you examples.
validates :price, :presence => true,
:format => { :with => /^(\$)?(\d+)(\.|,)?\d{0,2}?$/ }

Validate the format of a string of comma separated words with regex

I'm trying to validate a string of comma separated words from a text field in a ruby class using regex. The following should be valid:
word
word, word, word
word,word,word
And the following should be invalid:
word word word
I thought this would work
/([a-z]+){1}(,\s*[a-z]+)*/i
On Rubular, it seems to be valid, but when I validate in my class as follows, it accepts what should be invalid strings.
#tag_regex = /([a-z]+){1}(,\s*[a-z]+)*/i
validates :tags,
:allow_blank => true,
:format => { :with => #tag_regex, :message => "Invalid tag format." }
I'm not sure whether my problem lies in the regex or with the method of validation itself. Any help is appreciated.
You forgot to use ^(start of the string) and $(end of the string)
So,it should be
/^([a-z]+)(,\s*[a-z]+)*$/i
Without ^,$ it would match anywhere in between the string..With ^,$ you are making it match exactly

Simple rails format validation not firing

I'm building an app where users can create url slugs for their profile. To make sure the slugs are valid I've added a validation in the User model for slugs:
validates :slug, :uniqueness => true, :format => { :with => /[a-z]+/ }, :allow_nil => true, :allow_blank => true
However, validation seems to pass, regardless of what format the slug string is, for example:
u.slug = 'jlskdf .jc oi/slkdjfie\*asdf&(*&*ss%&'
=> "jlskdf .jc oi/slkdjfie\\*asdf&(*&*ss%&"
u.save
=> true
Apparently it doesn't matter what I change the regex to either, everything passes. I've tried this format as well:
validates_format_of :slug, :with => /[a-z]+/
which gives the same results. Anyone have any ideas of what could be happening?
Your regular expression isn't anchored, so the pattern matches as long as it contains at least one letter a-z. Anything else is valid. Add \A and \z to the beginning and end to prevent matching any substring within the larger input.
:with => /\A[a-z]+\z/

Rails custom validation based on a regex?

I have the following regex that I use in my routes.rb for /type-in-something-here
# A-Z, a-z, 0-9, _ in the middle but never starting or ending in a _
# At least 5, no more than 500 characters
In the routes this works well as:
match ':uuid' => 'room#show', :constraints => { :uuid => /[A-Za-z\d]([-\w]{,498}[A-Za-z\d])?/ }
I want to have this also as a validation so invalid records aren't created. So I added the following to room.rb:
validates_format_of :uuid, :with => /[A-Za-z\d]([-\w]{,498}[A-Za-z\d])?/i, :message => "Invalid! Alphanumerics only."
But this validates_format_of isn't working, and instead of adding an error it's allow the record to save.
Any ideas what's wrong?
Thanks
For validation purposes, remember to add the beginning and end of string markers \A and \Z:
validates_format_of :uuid, :with => /\A[A-Za-z\d]([-\w]{,498}[A-Za-z\d])?\Z/i
Otherwise your regex will happily match any string that contains at least a letter or a digit. For some reason Rails implicitly adds the boundaries in the routes. (Probably because it embeds the regex inside a larger one to match the entire URL, with explicit checks for / and the end of the URL.)
using something like this
validates :uuid, :format => {:with => /[A-Za-z\d]([-\w]{,498}[A-Za-z\d])?/i},
:message => "your message"
For more check this
validates :name, format: { with: /\A[a-zA-Z]+\z/,
message: "Only letters are allowed" }

How do you validate against more than one regex?

I have this validation:
validates :url, :uniqueness => true,
:format => { :with => /^(http:\/\/)?(www\.)?youtube\.com\/watch\?v=([a-zA-Z0-9_-]*)/}
I want to let url match either the regex above or another regex. How do I add this second regex?
You put both regexes into one regex using the 'bar' operator which does an 'or' for you:
/(^(http:\/\/)?(www\.)?youtube\.com\/watch\?v=([a-zA-Z0-9_-]*)|helloworld)/
Matches a URL or a string containing "helloworld"

Resources