Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

393
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda