티스토리 뷰
프론트엔드/Vue.js
[Vue3] 로그인 창 만들기 + 아이디, 비밀번호 유효성 검증 (vuetify3 v-text-field + typescript)
실전압축코딩 2022. 11. 9. 09:31https://next.vuetifyjs.com/en/api/v-text-field/
v-text-field API — Vuetify
API for the v-text-field component.
next.vuetifyjs.com
vuetify의 v-form validate를 이용하여 유효성 검증을 구현하였다.
v-text-filed의 rule과 v-form의 ref를 잡아주면 된다.

template
<template>
<div>
<v-app id="inspire" style="background-color:#FCF6F1;">
<!-- Login Component -->
<div class="d-flex justify-center" style="margin-top:350px">
<v-card width="450px">
<div class="pa-11">
<div style="text-align: center" class="mb-16 mt-5">
<h1>Login</h1>
</div>
<v-form class="pl-5 pr-5" ref="loginForm">
<v-text-field
class="mb-2"
label="ID"
:rules="userIdRule"
v-model="username"
prepend-inner-icon="mdi-account"
></v-text-field>
<v-text-field
class="mb-2"
:rules="userPassRule"
v-model="password"
prepend-inner-icon="mdi-lock"
type="password"
label="Password"
>
</v-text-field>
<v-btn
@click="submitLogin"
color="blue lighten-1 text-capitalize"
depressed
large
block
dark
class="mb-5"
style="height: 45px"
>
로그인
</v-btn>
</v-form>
</div>
</v-card>
</div>
</v-app>
</div>
</template>
v-form의 ref를 loginForm으로 잡아주었다.
v-text-filed 에서 검증할 rule을 설정하면 된다.
setup
setup () {
const username = ref<string>('')
const password = ref<string>('')
const loginForm = ref<any>(null)
const userIdRule = ref( [
(v: string) => !!v || '아이디는 필수 입력 사항입니다.',
(v: string) => /^(?=.*[a-zA-Z])(?=.*[0-9]).{5,25}$/.test(v) || '영문, 숫자 조합으로 5-20자리 입력해주세요.',
] )
const userPassRule = ref( [
(v: string) => !!v || '비밀번호는 필수 입력 사항입니다.',
(v: string) => /^(?=.*[a-zA-Z])(?=.*[0-9]).{5,25}$/.test(v) || '영문, 숫자 조합으로 5-20자리 입력해주세요.',
] )
async function submitLogin () {
let validate = await loginForm.value?.validate()
// 유효
if(validate.valid) {
...
} else { // 유효 x
return
}
}
}
rule에 정규식을 집어 넣는다.
typescript는 처음이라 v-form의 ref type 설정에서 계속 컴파일 오류가 발생했다.
일단 임시방편으로 type을 any로 설정했다.
정규식 출처: https://goddino.tistory.com/267
[js] 비밀번호 영문 숫자 조합 8자리 이상, 영문 숫자 특수기호 조합 8자리 이상 체크 (ft. 정규식)
비밀번호 유효성 검사에 사용되는 정규식입니다. 비밀번호 영문 숫자 조합 8자리 이상 (정규식) let regPass = /^(?=.*[a-zA-Z])(?=.*[0-9]).{8,25}$/; if (!regPass.test(password)) alert("영문, 숫자 조합으로 8-20자리
goddino.tistory.com
'프론트엔드 > Vue.js' 카테고리의 다른 글
[Vue] 기존에 생성된 Vue 앱의 route에 엑세스 하기 (0) | 2023.04.21 |
---|---|
[TS] 느낌표(!) non-null 단어 연산자 ( Object is possibly undefined 에러) (0) | 2022.11.18 |
Failed to load config "@vue/standard" to extend from. (0) | 2022.11.08 |
vuetify icon 확인 사이트 (0) | 2022.11.03 |
[vue] package.json 과 package-lock.json (0) | 2022.11.02 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- react fiber 3d
- 394 decode string
- react 3d
- next.js import glsl
- attempted import error: bvh_struct_definitions' is not exported from './gpu/bvhshaderglsl.js' (imported as 'bvhshaderglsl').
- vue reactive
- webpack glsl
- ts glsl
- react three fiber
- 394. decode string js
- react 3d text
- eslint
- Vue.js
- react leva
- [leetcode] 394. decode string
- react ref reative
- three.js leva
- vue
- leva
- react glsl
- react 3d 에니메이션
- react three fiber leva
- vue react
- 394. decode string javascript
- vue ref
- react vue
- vue3
- typescript gsls
- [leetcode] 394. decode string js
- react 3d animation
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함