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

145
Vistas
Validate UUID without RegExp

JavaScript Array.every() does not work the way I expected

I have been tasked with writing a basic check for GUID validity without using regex. So all I'm doing is checking if the non-dash characters in the guid string are hexa digits.

const isValidGUIDChar = (char) =>  !isNaN(+char) || "abcdef".includes(char.toLowerCase());

// let guid = "123";
// let guid = "abc"
let guid = "abc123";

let isGuidValid = guid.replaceAll('-', '').length === 32 && guid.split().every(isValidGUIDChar);

console.log(isGuidValid);  // returns true for either 'abc' or '123' but false for 'abc123'

The results, mention in comment above are counter-intuitive. The every() method seems to evaluate to:

"(every character must either be numeric), or (every character must be from among 'abcdef')",

whereas what I need, and what seems intuitive, would be for it to evaluate to:

"(every character) must (either be numeric from among 'abcdef')"

How can I accomplish the latter?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

  • Your .split() is missing "" (BTW, use [...guid] with Spread instead)
  • Make sure you actually test with 32 char after! you replace all dashes
  • Use it as a function isGuidValid(guid) - reusability, remember?

const isHEX = (ch) => "0123456789abcdef".includes(ch.toLowerCase());

const isGuidValid = (guid) => {
  guid = guid.replaceAll("-", ""); // Format it first!
  return guid.length === 32 && [...guid].every(isHEX);
};

console.log(isGuidValid("adec3dc3-7025-4905-b584-d9e64117f2fd"));  // true
console.log(isGuidValid("adec3dc3-7025-4905-b584-d9e64117f2fz"));  // false
console.log(isGuidValid("adec3dc3-7025-4905-b584-d9e64117f2f"));   // false

A more robust approach, taking in consideration the RFC4122 treating a GIUD/UUID as groups of 8-4-4-4-12 codepoint chars each would be:

const isHEX = h => !isNaN(parseInt(h, 16));
const RFC4122Len = [8, 4, 4, 4, 12];

const isGuidValid = (guid) => {

  // Must have 36 Chars per codepoint (32 + 4 dashes)
  const codePointLenght = [...guid].length;
  if (codePointLenght !== 36) return false;

  // Must have 5 groups
  const groups = guid.split("-");
  if (groups.length !== 5) return false;

  // RFC defines 5 groups of 8-4-4-4-12 codepoints chars each
  const lenMatch = groups.map(gr => [...gr].length).every((len, i) => len === RFC4122Len[i]);
  if (!lenMatch) return false;

  // Finally join groups (without the "-" separators and check for each char is HEX)
  return [...(groups.join(""))].every(isHEX);
};

console.log(isGuidValid("adec3dc3-7025-4905-b584-d9e64117f2fd")); // true
console.log(isGuidValid("adec3dc3-7025-4905-b584-d9e64117f2fz")); // false
console.log(isGuidValid("adec3dc3-7025-4905-b584-d9e64117f2f"));  // false

There's more to make it fully RFC4122 compliant but you got the general idea.

about 4 years ago · Juan Pablo Isaza Denunciar

0

To check if the characters are hex digits just use this 3 lines :

 function isHex(h)
 {
    let a = parseInt(h, 16);

    return a.toString(16) === h;
 }
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