0

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?

Sri Murthy Upadhyayula
  • 22,620
  • 1
  • 18
  • 20

2 Answers2

0

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 '?'.

Thaha kp
  • 3,689
  • 1
  • 26
  • 25
0

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

Community
  • 1
  • 1
trushkevich
  • 2,657
  • 1
  • 28
  • 37