Validate Numericality Conditions on English & Arabic Numbers - ruby-on-rails

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}

Related

Validate input text rails

How do I make a text field only accept letters in rails, enter a number by keyboard and do not reflect in the input.
I´m using client side validation for sever
validates_format_of :atr, :table, :with => /^[a-z]+$/i
/^[a-z]+$/i
will only allows letters only. The following is the details of the regex.
https://apidock.com/rails/ActiveModel/Validations/HelperMethods/validates_format_of
^ => Start of line
[a-z] => Any single character in the range a-z
+ => One or more characters of the range
$ => End of line
i => To make case insensitive

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 (おにぐるま).

Regular expression format for modal validations of a text field?

I am having a simple title field for a question in the database.
validate :title, :presence => true, :format => { :with => regex,
:message => "Invalid Title" }
I should make sure that the title of the question doesn't contain only numbers and special characters. It can however contain them along with alphabets. But I should not allow the user to enter only numbers and special characters in the title field.
For example:
Will the temperature cross 40 degrees ?
Is a valid question.
But,
12213232323
?$2112121212
?
are invalid question titles.
What would be the format regex for this?
Try this simple one,
[a-zA-Z].*\?$
This will check the presence of have one or more alphabets in the question. Also check the question is ending with '?'.
I would go with something like this /\A.*[a-zA-Z].*\z/ - this will let you have alphabets in any position.
p.s. don't use ^ and $ for determining start and end of an input string. Here is the explanation Difference between \A \z and ^ $ in Ruby regular expressions

Rails Validations, number + letters + spaces

Looking for a Rails validation that will only allow letters, numbers, and spaces.
This will do letters and numbers, but no spaces.
I need spaces.
validates_format_of :name, :with => /^\w+$/i,
:message => "can only contain letters and numbers."
validates_format_of :name, :with => /^[a-zA-Z\d ]*$/i,
:message => "can only contain letters and numbers."
Here is only Number, Letters ans spaces.
Is that exactly what you need ?
PS : This tools is very useful if you are doing a lot of reg-exp : http://rubular.com/
This is one way:
validates :name, format: { with: /\A[a-zA-Z0-9\s]+\z/i, message: "can only contain letters and numbers." }
Have a nice day.
If you only want letters, numbers, and spaces, but not underscores, the accepted answer won't work for you. The following also won't allow empty strings, but it wouldn't matter either way if the rails model has validates_presence_of :name
/^[a-z0-9 ]+$/i

Validate that string belongs to specific language alphabet

How can I validate Rails model string attribute that it belongs to specific language alphabet characters?
Thanks.
There's a library called whatlanguage that recognize the languages of the string, example:
require 'whatlanguage'
"Je suis un homme".language # => :french
Works with Dutch, English, Farsi, French, German, Swedish, Portuguese, Russian and Spanish out of the box, so it recognize Cyrillic too.
You'll want to validate the value of the attribute against a regular expression.
# Only match characters a-z
validates_format_of :attr, :with => /[a-z]/
validates_format_of seems to be the right thing for you. the documentation says:
Validates whether the value of the specified attribute is of the correct form by matching it against the regular expression provided.
class Person < ActiveRecord::Base
validates_format_of :email, :with => /\A([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create
end
Note: use \A and \Z to match the start and end of the string, ^ and $ match the start/end of a line.
A regular expression must be provided or else an exception will be raised.

Resources