3

We have a customer that wants to block / detect Non European character sets as Arabian and Chinese. People are now filling in our forms with content like this:

enter image description here

I have no clue what the best way to detect Non European character sets.

So I am wondering which way to go:

  • some kind of jQuery input validation
  • Rails validation
  • ...

Running:

  • ruby 1.9.3p551
  • Rails 3.2.19

Kr

FastSolutions
  • 1,809
  • 1
  • 25
  • 49
  • One JS approach is to use regex to check if the input contains non-English chars (e.g. http://stackoverflow.com/questions/13840143/jquery-check-if-special-characters-exists-in-string, http://stackoverflow.com/questions/150033/regular-expression-to-match-non-english-characters). Another way is to check the char codes like shown here http://jsfiddle.net/dekkard/o842p2p2/ – dekkard Jun 19 '15 at 13:21

3 Answers3

4

So after 4 hours of searching I have found this beauty:

/\p{Arabic}/u

And for multiple Encoding you can use:

/\p{Arabic}|\p{Han}/u

Tested via http://rubular.com/r/AKFhcpjIbL

Found via http://ruby-doc.org/core-2.1.1/Regexp.html

Full validation:

validates_format_of :first_name, :last_name, without: /\p{Arabic}/u

Interesting read in this context -> How can I detect CJK characters in a string in Ruby?

Community
  • 1
  • 1
FastSolutions
  • 1,809
  • 1
  • 25
  • 49
2

You can create a validation on your model that ensures the incoming model attributs is #ascii_only?

PhilVarg
  • 4,762
  • 2
  • 19
  • 37
0

You could do a rails validation in the model:

http://apidock.com/rails/ActiveModel/Validations/HelperMethods/validates_format_of

class YourModel< ActiveRecord::Base
 validates_format_of :your_field, without: /[^\u0000-\u007F]+/
end

\u0000-\u007F is the equivalent to the first 255 characters in UTF-8 or Unicode, which are always the ASCII characters.

gabrielhilal
  • 10,660
  • 6
  • 54
  • 81