ruby on rails - Validate Japanese Character in Active Record Callback -


i have japanese project needs validate half width , full width japanese character, 14 chars allowed on half width , 7 characters on full width.

is there knows how implement that?

right on model

class customer    validates_length_of :name, :maximum => 14 end 

is not choice

i'm using ror 2.3.5 both fullwidth , halfwidth can used

the following code may push on line fulfil exact requirement you've far specified in least possible time. uses moji gem (japanese documentation), gives lots of convenience methods in determining content of japanese language string.

it validates maximum of 14 characters in name only consists of half-width characters, , maximum of 7 characters names otherwise (including names contain combination of half- , full-width characters i.e. presence of 1 full-width character in string make whole string regarded "full-width").

class customer     validates_length_of :name, :maximum => 14,      :if => proc.new { |customer| half_width?(customer.name) }   validates_length_of :name, :maximum => 7     :unless => proc.new { |customer| half_width?(customer.name) }    def half_width?(string)     moji.type?(string, moji::han_kata)   end  end 

assumptions made:

  1. data encoding within system utf-8, , gets stored such in database; further necessary re-encoding (such passing data legacy system etc) done in module.
  2. no automatic conversion of half-to-full width characters done before data saved database i.e. half-width characters allowed in database reasons perhaps of legacy system integration, proper preservation of actual user input(!), and/or aesthetic value of half-width characters(!)
  3. diacritics in half-width characters treated own separate character (i.e. no parsing of カ , ゙ considered 1 character purposes of determining string length)
  4. there 1 name field specify , not, say, 4 (for surname, surname furigana, given name, given name furigana) quite common nowadays.

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -