# Password Complexity for Zod

**URL:** https://forum.codewithmosh.com/t/password-complexity-for-zod/23622
**Category:** Next.js
**Created:** [November 14, 2023, 12:20am UTC](https://forum.codewithmosh.com/t/password-complexity-for-zod/23622 "2023-11-14T00:20:20Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![ScorpionKing21](https://avatars.discourse-cdn.com/v4/letter/s/278dde/32.png) [@ScorpionKing21](https://forum.codewithmosh.com/u/ScorpionKing21)
#### Post date: [November 14, 2023, 12:20am UTC](https://forum.codewithmosh.com/t/password-complexity-for-zod/23622/1 "2023-11-14T00:20:20Z")

</div>

```auto
const schema = z
  .object({
    email: z.string().email(),
    password: z.string().min(8),
  })
  .superRefine(({ password }, checkPassComplexity) => {
    const containsUppercase = (ch: string) => /[A-Z]/.test(ch);
    const containsLowercase = (ch: string) => /[a-z]/.test(ch);
    const containsSpecialChar = (ch: string) =>
      /[`!@#$%^&*()_\-+=\[\]{};':"\\|,.<>\/?~ ]/.test(ch);
    let countOfUpperCase = 0,
      countOfLowerCase = 0,
      countOfNumbers = 0,
      countOfSpecialChar = 0;
    for (let i = 0; i < password.length; i++) {
      let ch = password.charAt(i);
      if (!isNaN(+ch)) countOfNumbers++;
      else if (containsUppercase(ch)) countOfUpperCase++;
      else if (containsLowercase(ch)) countOfLowerCase++;
      else if (containsSpecialChar(ch)) countOfSpecialChar++;
    }
    if (
      countOfLowerCase < 1 ||
      countOfUpperCase < 1 ||
      countOfSpecialChar < 1 ||
      countOfNumbers < 1
    ) {
      checkPassComplexity.addIssue({
        code: "custom",
        message: "password does not meet complexity requirements",
      });
    }
  });

```
