Home > other >  File Name Validation using formik yup
File Name Validation using formik yup

Time:03-24

I am applying file name validations using YUP with regex. It's showing an error every time when the file is uploaded, the file name should not start with special characters. Please refer to the codesandbox link: Link to Code and see line no. 22

CodePudding user response:

The regex you need will be ^[0-9a-zA-Z].*, a little explanation of why and how:

^ asserts position at start of the string

0-9 matches a single character in the range between 0 and 9 (case sensitive)

a-z matches a single character in the range between a and z (case sensitive)

A-Z matches a single character in the range between A and Z (case sensitive)

. matches any character (except for line terminators)

* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)

Now to make it work in your example you need to check if value.name will match said regex, this is how you can achieve this:

validationSchema: Yup.object().shape({
  file: Yup.mixed()
    .required("file is required")
    .test(
      "fileName",
      "file name should not start with special characters",
      (value) => {
        return value.name.match(/^[0-9a-zA-Z].*/);
      }
    )

Once you will try to upload something that will have special characters at the start it should show error.

I have updated also codesandbox, so please have a look

  • Related