Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

392
Views
File Name Validation using formik yup

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!