티스토리 뷰
react-form-hook 의 공식 예제들을 보면 컴포넌트들에 validation 설정과 에러메세지들이 주렁주렁 달려있는걸 보게 된다.
<input {...register("firstName", { required: true, maxLength: 20 })} />
<input {...register("lastName", { pattern: /^[A-Za-z]+$/i })} />
<input type="number" {...register("age", { min: 18, max: 99 })} />
<input {...register("mail", { required: "Email Address is required" })} />
<p>{errors.mail?.message}</p>
<input type="submit" />
...
react 하면서 좀 지저분하다고 느낀 부분이 html render 부분에 로직이 많이 들어가고 jquery 사용하는 느낌이랄까...
그래서 yup을 같이 쓴 걸 보고 이거다 싶었다.
https://www.npmjs.com/package/yup
yup은 이름이 직관적이지 않다.
'Yup is a JavaScript schema builder for value parsing and validation'
이번에 개발하면서 좀 복잡했던 패스워드 validation를 정리해봤다.
패스워드 조건은
const passwordErrMsg = '비밀번호는 영문+숫자 조합 10자리 이상 20자리 이하로 입력해주세요'
password: yup
.string()
.required('비밀번호를 입력해주세요')
.min(10, passwordErrMsg)
.max(20, passwordErrMsg)
.matches(/^(?=.*[0-9]+)[a-zA-Z][a-zA-Z0-9]+$/, passwordErrMsg)
.test('validPassword', '키보드 배열상 연속된 문자열은 사용할 수 없습니다', value => {
return value ? !pwContinueKeyboard(value as string) : true
}),
키보드 배열 연속은 function을 구현해서 설정했다.
계속 테스트 해보고 있는데 /^(?=.*\d+)(?=.*[a-zA-Z])[0-9a-zA-Z]{10,20}$/ 가 맞는듯
정규식 어려워
누군가에게 도움이 되면 좋겠군
'개발새발' 카테고리의 다른 글
해결: React does not recongnize the 'isColor' Prop on a DOM element. if you intentionally want to to appear in the DOM as a custom attribute, spell it as lowercas 'iscolor' instead. (0) | 2023.05.12 |
---|---|
[React] react-form-hook과 react-select의 궁합 (0) | 2022.08.17 |
이직 1년 (0) | 2022.08.16 |
새 직장, 새 업무 (0) | 2021.09.14 |
카프카 EC2에 securityGroup 설정 (0) | 2021.08.30 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 경제뉴스
- vue.js
- 개발언어
- AWS
- 자바
- JavaScript
- 이직
- 프로그래밍
- 개발자
- 코틀린
- Node.js
- 개발
- 공부
- 주식초보
- 번역
- Kotlin
- KAFKA
- Java
- 카프카
- mariadb
- 자바스크립트
- es6
- 주식
- Amplify
- auth
- openjdk
- 후기
- morningbrew
- 스터디
- 마리아DB
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함