Skip to content

Commit

Permalink
trim phone number value on change, update validation
Browse files Browse the repository at this point in the history
  • Loading branch information
DovileMel committed May 6, 2024
1 parent b06d0a9 commit a6a4299
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/components/forms/Registration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ const RegistrationForm = ({
error={errors.assignedTo}
onChange={(value: any) => {
setFieldValue('assignedTo', value);
setFieldValue('phone', value?.phone || '');
setFieldValue('phone', value?.phone?.trim() || '');
}}
options={users}
disabled={isCustomer}
Expand All @@ -334,7 +334,7 @@ const RegistrationForm = ({
error={errors.phone}
onChange={(e: any) => {
if (/^\+?[0-9\s]{0,11}$/.test(e)) {
setFieldValue('phone', e);
setFieldValue('phone', e.trim());
}
}}
disabled={isCustomer}
Expand Down
2 changes: 1 addition & 1 deletion src/pages/MyProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const MyProfile = () => {
label={inputLabels.phone}
name="phone"
placeholder="864222222"
onChange={(value) => setFieldValue('phone', value)}
onChange={(value) => setFieldValue('phone', value?.trim())}
disabled={disabled}
error={errors.phone}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/TenantUsers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ const NariaiPage = () => {
label={inputLabels.phone}
name="phone"
placeholder="864222222"
onChange={(value) => setFieldValue('phone', value)}
onChange={(value) => setFieldValue('phone', value?.trim())}
disabled={disabled}
error={errors.phone}
/>
Expand Down
8 changes: 5 additions & 3 deletions src/utils/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { validationTexts } from './texts';
import { FishBatch, FishType } from './types';
import { checkIfDateIsAfter } from './functions';

const lithuanianPhoneNumberFormat = /^(06|86|\+3706)\d{7}$/;

export const loginSchema = Yup.object().shape({
email: Yup.string().required(validationTexts.requireText).email(validationTexts.badEmailFormat),
password: Yup.string().required(validationTexts.requireText),
Expand All @@ -29,7 +31,7 @@ export const validateNewTenantUser = Yup.object().shape({
phone: Yup.string()
.required(validationTexts.requireText)
.trim()
.matches(/^(86|\+3706)\d{7}$/, validationTexts.badPhoneFormat),
.matches(lithuanianPhoneNumberFormat, validationTexts.badPhoneFormat),
personalCode: Yup.string()
.required(validationTexts.requireText)
.trim()
Expand All @@ -45,7 +47,7 @@ export const validateMyProfile = Yup.object().shape({
phone: Yup.string()
.required(validationTexts.requireText)
.trim()
.matches(/^(86|\+3706)\d{7}$/, validationTexts.badPhoneFormat),
.matches(lithuanianPhoneNumberFormat, validationTexts.badPhoneFormat),
});

export const validateFishStocking = (minTime: number) =>
Expand All @@ -60,7 +62,7 @@ export const validateFishStocking = (minTime: number) =>
phone: Yup.string()
.required(validationTexts.requireText)
.trim()
.matches(/^(86|\+3706)\d{7}$/, validationTexts.badPhoneFormat),
.matches(lithuanianPhoneNumberFormat, validationTexts.badPhoneFormat),
batches: Yup.array().of(
Yup.object().shape({
fishType: Yup.object()
Expand Down

0 comments on commit a6a4299

Please sign in to comment.