Skip to content

Commit

Permalink
added form validations (#621)
Browse files Browse the repository at this point in the history
Co-authored-by: JR Frazier <[email protected]>
  • Loading branch information
jr-frazier and JR Frazier authored Oct 16, 2024
1 parent 51372a3 commit a60b342
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/components/forms/apply-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Input from "@ui/form-elements/input";
import Button from "@ui/button";
import { hasKey } from "@utils/methods";
import Feedback from "@ui/form-elements/feedback";
import { linkedinRegex, githubRegex } from "@utils/formValidations";

interface IFormValues {
firstName: string;
Expand Down Expand Up @@ -280,6 +281,11 @@ const ApplyForm = () => {
showState={!!hasKey(errors, "linkedInAccountName")}
{...register("linkedInAccountName", {
required: "LinkedIn Account Name is required",
pattern: {
value: linkedinRegex,
message:
"Please enter your Linkedin profile URL. For example, linkedin.com/in/your-name",
},
})}
/>
</div>
Expand All @@ -303,6 +309,11 @@ const ApplyForm = () => {
showState={!!hasKey(errors, "githubAccountName")}
{...register("githubAccountName", {
required: "GitHub Account Name is required",
pattern: {
value: githubRegex,
message:
"Please enter your Github profile URL. For example, github.com/your-name",
},
})}
/>
</div>
Expand Down
2 changes: 2 additions & 0 deletions src/components/forms/mentor-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Input from "@ui/form-elements/input";
import Button from "@ui/button";
import { hasKey } from "@utils/methods";
import Feedback from "@ui/form-elements/feedback";
import { validateProfileLink } from "@utils/formValidations";

interface IFormValues {
name: string;
Expand Down Expand Up @@ -159,6 +160,7 @@ const MentorForm = () => {
{...register("github-portfolio-or-linkedin", {
required:
"GitHub Portfolio or LinkedIn is required",
validate: validateProfileLink,
})}
/>
</div>
Expand Down
28 changes: 28 additions & 0 deletions src/utils/formValidations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// ^(https?:\/\/)? : Matches an optional "http" or "https" at the start, followed by "://". The "?" makes it optional.
// (www\.)? : Matches the optional "www." subdomain.
// github\.com\/ : Matches "github.com/" exactly. The backslash escapes the dot.
// [A-Za-z0-9_-]+ : Matches one or more alphanumeric characters, underscores, or hyphens (used for GitHub usernames).
// \/? : Matches an optional trailing forward slash.
// $ : End of the string, ensuring there are no extra characters after the valid GitHub username.

export const githubRegex: RegExp =
/^(https?:\/\/)?(www\.)?github\.com\/[A-Za-z0-9_-]+\/?$/;

// ^(https?:\/\/)? : Matches an optional "http" or "https" at the start, followed by "://". The "?" makes it optional.
// (www\.)? : Matches the optional "www." subdomain.
// linkedin\.com\/in\/ : Matches "linkedin.com/in/" exactly. The backslash escapes the dots.
// [A-Za-z0-9_-]+ : Matches one or more alphanumeric characters, underscores, or hyphens (used for LinkedIn profile URLs).
// \/? : Matches an optional trailing forward slash.
// $ : End of the string, ensuring there are no extra characters after the valid LinkedIn profile URL.
export const linkedinRegex: RegExp =
/^(https?:\/\/)?(www\.)?linkedin\.com\/in\/[A-Za-z0-9_-]+\/?$/;

export function validateProfileLink(url: string): string | boolean {
if (githubRegex.test(url)) {
return true;
} else if (linkedinRegex.test(url)) {
return true;
} else {
return "The value entered is not a valid Linkedin or Github profile link";
}
}

0 comments on commit a60b342

Please sign in to comment.