Rails validates with regex is not working? - ruby-on-rails

Simple regex is not working.. For example first_name = "a11" works fine. Why isn't my format regex validating properly?
validates :first_name, presence: { message: "Name cannot be blank." },
format: { with: /[a-z]/i, message: "Name must only contain letters." },
length: { minimum: 2, message: "Name must be at least 2 letters." }

Because it matches with you regex.
You have to specify the begin and end of the string, and add * or it will just match one char.
format: { with: /\A[a-z]*\z/i, message: "Name must only contain letters." },
Also note don't use ^ and $, in ruby, ^ and $ matches the begin and end of a line, not the string, so it will be broken on multiline strings.

Your regex returns a match. It's looking for any letter that's present anywhere in the string and returns that match. You want to specify that only letters are allowed.
Update Original answer insecure: https://stackoverflow.com/a/17760113/836205
If you change it to /^[a-z]*$/ /\A[a-z]*\z/ it will only match a string that contains all lower case letters.

Related

Rails: Validation with lots of format

I want to validate a Nickname but I have a lot of format like this:
validates :nickname, presence: true, unniqueness: true, format: { with: /\A[a-zA-Z0-9]+\Z/ }, format: { without: /\s/ }, format: { without: /[!-\/\#\^\~\`\(\)\[\]\>\<\=]/ }
warning: key :format is duplicated and overwritten on line 38
warning: key :format is duplicated and overwritten on line 38
Obviously down't work in this way, how can I solve it? Thank you
Your first regex covers everything:
format: { with: /\A[a-zA-Z0-9]+\Z/ }
but you probably want \z instead of \Z to avoid issues with trailing newlines. Anything that matches /\A[a-zA-Z0-9]+\z/ won't contain any space characters so the /\s/ test is already covered, similarly for the punctuation test.
Also, you've misspelled uniqueness as unniqueness so you'll want to fix that too.
That would leave you with just:
validates :nickname, presence: true, uniqueness: true, format: { with: /\A[a-zA-Z0-9]+\Z/ }
If you really did have multiple regexes to test then you could do it in a custom method:
validate :nickname_format
def nickname_format
return if(!nickname) # The `presence: true` takes care of complaining about this.
if(nickname ~! ...)
errors.add(:nickname, 'blah blah')
elsif(...)
...
end
end
so that you could check each regex individually.

Regexp in format: validation isn't stopping the right characters

I'm new to regex and I'm trying to only allows letters, numbers, quotes and the following characters: !.:?!_+=, -
I have the validation below in a guide modal. I can name a guide just '&' if I want and it accepts it, from my understanding this validation should stop me.
validates :name, presence: true, length: { maximum: 255 }, uniqueness: { case_sensitive: false },
format: { with: /[a-zA-Z 1-9 0!.'":?!_+=, -]/, message: "only allows letters, numbers, quotes and !.:?!_+=, -" }
Not sure what is going wrong, I tested out the regex in Rubular.com and it works in there.
Edit
After testing it, it turns out it stops '&' but lets '&11' pass. If an invalid key is with a valid key it passes. Maybe I'm using format: wrong or shouldn't be using format: for this?
I've simplified a bit your regex, added proper starting/ending line markers and added a + at the end to match if there is one or more char in the input. Here's what it looks like:
/\A[a-zA-Z0-9 !.'":?!_+=,-]+\z/
If you want to have a cleaner one:
/\A[\w !.'":?!_+=,-]+\z/
Since \w matches [a-zA-Z0-9]

Regular expression trouble in rails 4

I've the following regex in my model
validates :profile_name, presence:true,
uniqueness:true,
format:
{
with: /^[a-zA-Z0-9_-]+$/,
message: "Must be formatted correctly"
}
My tests are not passing because of regex. I am validating the profile name.
Here is my error log:
The provided regular expression is using multiline anchors (^ or $),
which may present a security risk. Did you mean to use \A and \z, or
forgot to add the :multiline => true option? (ArgumentError)
I think am doing right, still it is not passing. Please Help me to fix this.
Use, \A and \z instead of ^ and $ for regex:
validates :profile_name, presence:true,
uniqueness:true,
format:
{
with: /\A[a-zA-Z0-9_-]+\z/,
message: "Must be formatted correctly"
}
Read more on Regular Expressions as to why.

Devise validation constantly failing on sign up

Making a simple Ruby on Rails app as practise that requires a user to sign up.
Everything works well until I implement regex validation on a 'profile_name' field
Here's my 'user' model:
validates :profile_name, presence: true,
uniqueness: true,
format: {
with: /^a-zA-Z0-9_-$/,
message: 'Must be formatted correctly.'
}
And yet the profile name 'jon' simply refuses to pass. Where could this error be coming from, other than my 'user' model?
You need to add brackets around the ranges so that the regex matches "any of the range" rather than "all of the range in order". Put a + on the end to allow it to match anything in the range more than once.
You also need to change your beginning and ending of lines to beginning and ending of strings!
validates :profile_name, presence: true,
uniqueness: true,
format: {
with: /\A[a-zA-Z0-9_-]+\z/,
message: 'Must be formatted correctly.'
}
Details:
\A # Beginning of a string (not a line!)
\z # End of a string
[...] # match anything within the brackets
+ # match the preceding element one or more times
Really useful resource for generating and checking regex: http://www.myezapp.com/apps/dev/regexp/show.ws
Try like this , It is working fine
validates :name, presence: true,
uniqueness: true,
format: {
with: /\A[a-zA-Z0-9_-$]+\z/,
message: 'Must be formatted correctly.'
}
I just tested your regular expression in Rubular with the 'jon'. There is no matching.
I am not optimized regular expression coder. But still the below regular expression works.
/^[a-zA-Z0-9_-]+$/
So try
validates :name, presence: true,
uniqueness: true,
format: {
with: /^[a-zA-Z0-9_-]+$/,
message: 'Must be formatted correctly.'
}

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

Resources