Skip to content

Commit

Permalink
refactor:#5 학번 유효성 검사 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
dalzzy committed Jan 4, 2025
1 parent d7df8cb commit ebb38fb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
13 changes: 4 additions & 9 deletions src/components/SignUp/StudentIdStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useRecoilState } from 'recoil';
import { useState } from 'react';
import { signupAtom } from '@/recoil/atoms/userAtom';
import InputErrorMessage from '../common/Error/InputErrorMessage';
import { isStudentIdValid, validateStudentId } from '@/utils/validate-input';

const StudentIdStep: React.FC<{ onNext: () => void }> = ({ onNext }) => {
const [signupState, setSignupState] = useRecoilState(signupAtom);
Expand All @@ -18,17 +19,11 @@ const StudentIdStep: React.FC<{ onNext: () => void }> = ({ onNext }) => {
const handleInputChange = (value: string) => {
setSignupState((prev) => ({ ...prev, studentId: value }));

if (!/^\d*$/.test(value)) {
setErrorMessage('숫자만 입력 가능합니다.');
} else if (value.length < 9) {
setErrorMessage('올바른 학번을 입력해주세요.');
} else {
setErrorMessage('');
}
const validationError = validateStudentId(value);
setErrorMessage(validationError || '');
};

const isFormValid =
signupState.studentId.length === 9 && /^\d+$/.test(signupState.studentId);
const isFormValid = isStudentIdValid(signupState.studentId);
return (
<div>
<MainTitle>학교 인증을 위해</MainTitle>
Expand Down
18 changes: 18 additions & 0 deletions src/utils/validate-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,21 @@ export const validateNickname = (value: string): string | null => {
export const isNicknameValid = (value: string): boolean => {
return validateNickname(value) === null;
};

{
/* 학번 */
}

export const validateStudentId = (value: string): string | null => {
if (!/^\d*$/.test(value)) {
return '숫자만 입력 가능합니다.';
}
if (!/^20[12]\d{6}$/.test(value)) {
return '올바른 학번을 입력해주세요.';
}
return null;
};

export const isStudentIdValid = (value: string): boolean => {
return validateStudentId(value) === null;
};

0 comments on commit ebb38fb

Please sign in to comment.