Implementation of validation using regular expressions

Overview

This time, I will summarize how to implement validation using regular expressions.

First, let's take a quick look at validation and regular expressions.

Validation

By validating when saving the data, it will play invalid data. You can't register empty data, specify characters that must be included in the character string, limit the number of characters, etc. There are various validations.

As a typical example, let's validate it so that it will not be empty when entering the nickname.

models/user.rb


---abridgement---
validates :nickname, presence: true
---abridgement---

Validate each input item like this.

Regular expressions

A regular expression is a syntax that combines symbols and character strings, and can extract a specified character string pattern from the sentence to be searched.

If you use it often, \ A ... Matches the beginning of a string \ z ... matches the end of the string There are some others like this.

If you search, you will find a lot, so you don't need to remember it.

Validation using regular expressions

What I would like to summarize this time is validation using regular expressions.

Specifically, we will implement validation that the data entered in the password will be half-width alphanumeric characters.

The point is to assign a regular expression to a variable and incorporate it into validation.

models/user.rb


  validates :password, presence: true
  validates :password, length: { minimum: 6 }
  VALID_PASSWORD_REGIX = /\A[a-z0-9]+\z/i
  validates :password, format: { with: VALID_PASSWORD_REGIX }

From top to bottom ・ Password is required ・ Password must be 6 characters or more ・ Password must be single-byte alphanumeric characters Validation is organized.

Recommended Posts

Implementation of validation using regular expressions
[Java] Summary of regular expressions
Regular expressions
[Java] Comparison method of character strings and comparison method using regular expressions
Validation of log messages using mockito
[Android] Implementation of side-scrolling ListView using RecyclerView
Name a group of regular expressions (Java)
Implementation of user authentication function using devise (2)
Implementation of user authentication function using devise (1)
Implementation of tabs using TabLayout and ViewPager
Implementation of user authentication function using devise (3)
Regular expressions that match 99% of email addresses
[Rails] Implementation of validation that maintains uniqueness
Implementation of GKAccessPoint
[Rails] Implementation of search function using gem's ransack
[Rails 6] Implementation of inquiry function using Action Mailer
[Rails] Implementation of image enlargement function using lightbox2
Match IP addresses using regular expressions in Java
[Rails] Implementation of batch processing using whenever (gem)
[Rails] Implementation of PV number ranking using impressionist
[Rails] Implementation of image slide show using Bootstrap 3
[Rails] Set validation for the search function using Rakuten API (from the implementation of Rakuten API)
Implementation of Google Sign-In using Google OAuth 2.0 authentication (server edition)
Implementation of flash messages
Implementation of search function
Example of using vue.config.js
Notes on regular expressions
[Rails] Implementation of multi-layer category function using ancestry "Preparation"
Applied implementation of chat-space
Summary of using FragmentArgs
[Rails] Implementation of multi-layer category function using ancestry "seed"
Implementation of pagination function
colorize and regular expressions
Summary of using DBFlow
Database implementation using Realm
[Rails] Implementation of multi-layer category function using ancestry "Editing form"
[Rails] Implementation of multi-layer category function using ancestry "Creation form"
[Rails] Implementation of tagging function using intermediate table (without Gem)
Handling of line beginning and line ending in regular expressions in Ruby
Example of params using where
Rails implementation of ajax removal
[Swift] Simple implementation of UIImageView
Summary of using Butter Knife
[Swift] Implementation of ultra-simple billing
[Rails 6] Implementation of search function
Example of using abstract class
Implementation of image preview function
[Java] Implementation of Faistel Network
Signing and validation using java.security.Provider
Implementation of XLPagerTabStrip with TabBarController
[Rails] Implementation of category function
About regular expressions in Ruby
Rate-Limiting using RateLimiter of Resilience4j
Implementation of category pull-down function
Implementation of unit test code
Implementation of gzip in java
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
Implementation of tri-tree in Java
Learn regular expressions little by little ①
Implementation of HashMap in kotlin