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]
Related
I am new with rails and I am trying to register my user's names without special characters and spaces but allowing the - symbol.
Example :
##Pierre! => invalid
Pierre-Louis => valid
Pierre Louis => invalid
In the User.rb file I have :
class User < ApplicationRecord
...
validates :first_name, presence: true, length: {maximum: 25}, format: {with: Regexp.new('[^0-9A-Za-z-]')}
...
I don't have any error message and I'm still able to register using special characters.
Also, I'd like to add an error message if the name isn't validated.
Do you have any leads ?
Any help would be much appreciated !
When you validate format with regex, you need to ensure you match the whole string that is being validated. In order to do that your regex needs to start with \A and end with \z that are anchors marking beginning and ending of the string.
In your case:
validates :first_name,
presence: true,
length: {maximum: 25},
format: {with: /\A[-0-9A-Za-z]\z'/}
message: "Custom message"
assuming you accept numbers, small and big letters and a minus sign.
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.
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.
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.
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.'
}