Regular expression format for modal validations of a text field? - ruby-on-rails

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

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

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}

How to validate user's input for English alphabet only in Rails?

Im experimenting on a rails app . I would like to Validate user's input for English alphabet characters only in rails. Let's say we have a sign up form for user fill in their: Name, Address, Email address. But i would like them to fill the Address form with English alphabet only, Because some of them might accidentally fill in with Chinese alphabet which is not what we want.
Can we sort this out by a regular expression ? if so please guide me to write those three cases : (Any other ways are very welcome)
1: validate "English alphabet only "
2: Validate "Chinese alphabet is not allowed "
Your help will be very much appreciated !
Use validates_format_of:
validates_format_of :address, :with => /^[A-Za-z0-9 ]*$/
You can use validates with format:
validates :address, format: { with: /[a-zA-Z0-9]/}

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

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