[RUBY] Validated "up to 6 alphanumeric characters", "only full-width characters can be used", and "an error will occur if left blank"

2020/10/15 There is a mistake in this article. to correct.

Conclusion

I used the following.

with_options Conditional validation can be grouped. Enclose it in do end and use it. presence: true Make sure it is not empty. format: Verify that the regular expression described in the with option matches the sent value.

/ \ A [A-n-n-ichi-龥] + \ z /

Check if it matches Hiragana, Katakana, and Kanji within the range in Unicode. /[a-z\d]{6,}/i Check if it is case-sensitive and has at least 6 letters or numbers. If you set {6,20}, you can specify that there are no more than 6 characters and no 20 characters. message Describe the message that appears when an error occurs here.

code

models/user.rb


  with_options presence: true, format: { with: /\A[Ah-Hmm-One-龥]+\z/, message: 'Please use double-byte characters for' } do
    validates :family_name
    validates :first_name
  end

  validates :password, format: { with: /[a-z\d]{6,}/i, message: "Can use 6 or more alphanumeric characters" }

end

References

See the format helper section of the Rails guide. https://railsguides.jp/active_record_validations.html List of basic regular expressions https://murashun.jp/blog/20190215-01.html

Recommended Posts

Validated "up to 6 alphanumeric characters", "only full-width characters can be used", and "an error will occur if left blank"
The @Rule'mActivityRule' must be public. If you convert the Espresso test to Kotlin, an error will occur.