Ruby regex validation? - ruby-on-rails

In a Rails app, I have this current Regex validator below:
validates :characters, format: {with: /\A(([a-z0-9])+(-?[a-z0-9]+)*\s?)+\Z/, message: "can't be blank. Characters can only be [a-z 0-9 . # - +]" }
My validation for Characters initially only allowed lowercase letters and digits. Now I would like to allow for extra characters . # - + how do I structure my Regex now?

As per your question if you want to allow a-z , 0-9 and .#-+ only the regex for that would be:
/[a-z0-9.#+\-]/ and your validation will look something like this:
validates :characters, format: {with: /[a-z0-9.#+\-]/, message: "can't be blank. Characters can only be [a-z 0-9 . # - +]" }
you can even try that out at http://rubular.com/ . imho thats the best place to go for ruby regex.

Related

How to validate text input so that it only allows a-z, A-Z, 0-9, ();:?!,.[]{}- characters(also space) and in Rails?

How to validate text input so that it only allows a-z, A-Z, 0-9, ();:?!,.[]{}- characters(also space)?
I added something like in my model
class Oder < ApplicationRecord
......
......
validates :text, presence: true, format: { with: /[0-9\w]*[\(\;\:\-\?\!\,\.\[\]\{\}\,\.)]*/ }
......
end
end
But it is not working. It allows other characters(wïth ûmlauts) also.
Thanks in advance.
At the moment the regexp matches when one or more characters are in your text somewhere. You need to change the regexp to only match when the characters are the only characters between the start of the string (\A) and its end (\z).
Change your regexp to:
/\A[0-9\w\s\(\;\:\-\?\!\,\.\[\]\{\}\,\.)]*\z/
validates_format_of :text, without: /[-0-9\w ;:,.?!\[\]{}()]*/

Validate Numericality Conditions on English & Arabic Numbers

I'm using the following code to validate numericality in Rails:
validates :number, :numericality => {:greater_than_or_equal_to => 500}
And it works well, but then I added regex to allow Arabic numbers like this:
validates :number, :format =>{:with => /[0-9\u0660-\u0669]*/}
Here it accepts Arabic numbers but the condition greater_than_or_equal_to => 500 is working only on English numbers and I need it to support Arabic numbers too.
Alright, the digit can be defined as [0-9] and combined with Arabic ones, [0-9\u0660-\u0669]. We want to match 500 up to 999 first. We need to combine [5-9] with [\u0665-\u0669] -> [5-9\u0665-\u0669] that will match 5 to 9 in both ASCII and Arabic notations. After 5-9, there can be 2 more any digits, so we need to append [0-9\u0660-\u0669]{2} to it.
Next, we need to allow numbers more than 999, that is, 1000 and more. So, we need to add an alternative branch [0-9\u0660-\u066‌​9]{4,}.
The whole result is
/\A([5-9\u0665-\u0669][0-9\u0660-\u0669]{2}|[0-9\u0660-\u066‌​9]{4,})\z/
Where \A matches the start of string, \z matches the end of the string and (Branch_1|Branch_2) is a grouping construct that may be turned into a non-capturing one by adding ?: after the initial (:
/\A(?:[5-9\u0665-\u0669][0-9\u0660-\u0669]{2}|[0-9\u0660-\u066‌​9]{4,})\z/
See the regex demo
For ROR users who want to avoid using regex in such case to allow Arabic numbers to be working with Integer DB field (not string), I've found here a good example to convert Unicode numbers to Integer before validation, so it's still possible to use such condition:
validates :number, :numericality => {:greater_than_or_equal_to => 500}

ruby regex allow A-Z, a-z, 0-9, periods, hyphens, and spaces

I am trying to write a regex pattern with rubular to allow a person to add their company name like so:
validates :name,
presence: true,
length: { minimum: 5 },
format: { with: /\A[a-zA-Z\d\s.-]*\z/, message: :bad_format }
However, this is not working. I want to allow all A-Z, a-z, 0-9, spaces, periods, and hyphens. I want to make sure to reject anything else including line breaks.
Use \A ans \z as string borders instead ^ and $ (line borders).
Use space instead \s (includes line break).
Rubular

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]

How can I include Polish letters in a validation regex?

I have validation for first_name:
validates :first_name, :format => {:with => /\A[a-zA-Z]+\z/}
Can somebody tell me how to add letters like:
ą,ż,ź,ć,ń,ł,ś,ę,ó, Ą,Ż,Ź,Ć,Ń,Ł,Ś,Ę,Ó
I think you need Oniguruma character classes. To verify that string consists of unicode letters, use alpha character class.
"abcÓ" =~ /\A[[:alpha:]]+\z/ # => 0
"abcÓ1" =~ /\A[[:alpha:]]+\z/ # => nil # contains digit
This, of course, will include not only said polish letters, but all unicode letters. Including japanese kana, for example (おにぐるま).

Resources