ruby on rails - Regex to validate string having only characters (not special characters), blank spaces and numbers -
i using ruby on rails 3.0.9 , validate string can have characters (not special characters - case insensitive), blank spaces , numbers.
in validation code have:
validates :name, :presence => true, :format => { :with => regex } # here should set 'regex'
how should state regex?
there couple ways of doing this. if want allow ascii word characters (no accented characters Ê or letters other alphabets Ӕ or ל), use this:
/^[a-za-z\d\s]*$/
if want allow numbers , letters other languages ruby 1.8.7, use this:
/^(?:[^\w_]|\s)*$/u
if want allow numbers , letters other languages ruby 1.9.x, use this:
^[\p{word}\w\s-]*$
also, if planning use 1.9.x regex unicode support in ruby on rails, add line @ beginning of .rb file:
# coding: utf-8
Comments
Post a Comment