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

183
Vistas
A regex to allow exactly 5 digit number with one optional white space before and after the number?

With the regex in the jQuery code below, I can restrict the input text field to accept only 5 digits but it allows more white spaces before and after the number than just one. I mean to match a 5-digit number that is at the beginning of the string or is preceded by at most one space and is at the end of the string or is followed by at most one space. Please help me refine my regex to meet my requirement.

jQuery("input:text[name='address_1[zip]']").change(function(e) {
if (!jQuery(this).val().match(/\b\d{5}\b/g)) { 
        alert("Please enter a valid zip code");
        jQuery("input[name='address_1[zip]']").val("");
        return false;                        
    }        
});
about 4 years ago · Santiago Gelvez
3 Respuestas
Responde la pregunta

0

I might suggest just using lookarounds here:

/(?<![ ]{2})\b\d{5}\b(?![ ]{2})/

This pattern says to:

(?<![ ]{2})  assert that 2 (or more) spaces do NOT precede the ZIP code
\b\d{5}\b    match a 5 digit ZIP code
(?![ ]{2})   assert that 2 (or more) spaces do not follow

Here is a demo showing that the pattern works.

about 4 years ago · Santiago Gelvez Denunciar

0

You need to parse the beginning of line and end of line

match(\A\s?\d{5}\s?\z/g)

This mean:

  • \A begin of string
  • \s? One optional space
  • \d{5} five digits
  • \s? One optional space
  • \z end of string
about 4 years ago · Santiago Gelvez Denunciar

0

I think this will do.

/^\s?\d{5}\s?$/

Here's the pure JavaScript test code. You will have to convert this into JQuery style.

let p = /^\s?\d{5}\s?$/
let input = ' 54525 ';
let match = p.test( input );
console.log( match )
about 4 years ago · Santiago Gelvez 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