LoginSignup
2
1

More than 1 year has passed since last update.

[SpringMVC]値を正規表現でバリデーションする

Last updated at Posted at 2021-09-08

Tipsです。手短に。

@Patternアノテーションを利用して、クラスプロパティの値を正規表現でチェックする方法を紹介します。
よかったら いいね だけお願いします。

手順

  1. build.gradleにライブラリを追記
  2. フィールドにアノテーションを追記

build.gradleにライブラリを追記

build.gradle
dependencies {
    implementation 'org.hibernate.validator:hibernate-validator'
}

フィールドにアノテーションを追記

Sample.java
import javax.validation.constraints.Pattern;

public class Sample {

    @Pattern(regexp="^[a-zA-Z0-9]{3}", message="3文字でおねシャス")  
    String value;
...

他のアノテーションも知りたい

こちらの投稿が大変参考になります。
Spring Frameworkで使用できるBean Validationアノテーション一覧 - ぺんぎんらぼ

参考

タグ

  • Spring
  • SpringBoot
  • アノテーション
  • Annotation
  • Bean Validation
  • バリデーション
  • 正規表現
  • チェック
  • 方法
  • フォーマット
  • 検証
  • プロパティ
  • フィールド
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1