Skip to content

Commit

Permalink
adding title validation
Browse files Browse the repository at this point in the history
  • Loading branch information
djtyrf312 committed Jan 17, 2025
1 parent 31639dd commit 63958a2
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const todosWithUser: TodoWithUser[] = todosFromServer.map(
},
);
const defaultUserId = '0';
const titleRegexp = /^[a-zA-Zа-яА-ЯёЁїЇіІєЄґҐ0-9\s]*$/;

const validateTitle = (title: string): boolean => titleRegexp.test(title);

export const App = () => {
const [todos, setTodos] = useState<TodoWithUser[]>([...todosWithUser]);
Expand All @@ -25,6 +28,10 @@ export const App = () => {
const [hasTitleError, setHasTitleError] = useState(false);
const [hasUserError, setHasUserError] = useState(false);
const isNotSelectedUser = userId === defaultUserId;
const hasForbiddenSymbolsInTitle = !validateTitle(title);
const titleErrorMessage = hasForbiddenSymbolsInTitle
? 'Title contains special symbols'
: 'Please enter a title';

const clearForm = () => {
setTitle('');
Expand All @@ -35,7 +42,7 @@ export const App = () => {
event: React.FormEvent<HTMLFormElement>,
): null | void => {
event.preventDefault();
if (!title) {
if (!title || hasForbiddenSymbolsInTitle) {
setHasTitleError(true);
}

Expand Down Expand Up @@ -82,7 +89,7 @@ export const App = () => {
setHasTitleError(false);
}}
/>
{hasTitleError && <span className="error">Please enter a title</span>}
{hasTitleError && <span className="error">{titleErrorMessage}</span>}
</div>

<div className="field">
Expand Down

0 comments on commit 63958a2

Please sign in to comment.