2
1

More than 3 years have passed since last update.

Vuetifyのバリデーションルールの設定

Posted at
data
email: '',
emailConfirm: '',
rules: {
  required: value => !!value || '必須項目です',
  email: value => {
    const pattern = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    return pattern.test(value) || 'メールアドレスが間違っています'
  },
  emailMatch: value => value === this.email || 'メールアドレスが一致しません',
},
確認用のinputのrulesプロパティ
:rules="[rules.required, rules.emailMatch]"

これでうまくいけば良いけど、
コンポーネントの構造の問題なのかアロー関数のthis絡みのエラーが起きるケースがあった

Error in beforeMount hook: "TypeError: Cannot read property 'email' of undefined"

thisを使っているものをアローをやめてmethodsに移動すると解決

methods
emailMatchRule (value) {
  return value === this.email || 'メールアドレスが一致しません';
}
確認用のinputのrulesプロパティ
:rules="[rules.required, emailMatchRule]"
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