Javascript validate string regex

Viewed 43

I have this validate functions:

function unformat (value) {
  if (!value) {
    return '';
  }

  return value.replace(/^0+|[^0-9kK]+/g, '').toUpperCase();
}


function validate(value) {
  const unformatted = unformat(value);

  if (/^0+/.test(unformatted)) {
    return false;
  }

  let remainer = parseInt(unformatted.slice(0, -1), 10);
  let module = 1;
  let counter = 0;

  while (remainer > 0) {
    module = (module + (remainer % 10) * (9 - counter++ % 6)) % 11;
    remainer = Math.floor(remainer / 10);
  }

  const verifier = module > 0 ? '' + (module - 1) : 'K';

  return verifier === unformatted.slice(-1);

}

console.log(validate('14.211.109-8-')); // true, it should be false
console.log(validate('14.211.109-8 ')); // true, it should be false

https://jsfiddle.net/pmiranda/0nb51myp/5/

The idea is ot get false in two case when right now is getting true:

  1. When the string ends in `` // -
  2. When the string ends in // blankspace

How could I modify that validate function?

3 Answers

You can add a check at the beginning of the function:

if(/[ -]$/.test(value)) return false;

function unformat (value) {
  if (!value) {
    return '';
  }

  return value.replace(/^0+|[^0-9kK]+/g, '').toUpperCase();
}


function validate(value) {
  if(/[ -]$/.test(value)) return false;
  const unformatted = unformat(value);

  if (/^0+/.test(unformatted)) {
    return false;
  }

  let remainer = parseInt(unformatted.slice(0, -1), 10);
  let module = 1;
  let counter = 0;

  while (remainer > 0) {
    module = (module + (remainer % 10) * (9 - counter++ % 6)) % 11;
    remainer = Math.floor(remainer / 10);
  }

  const verifier = module > 0 ? '' + (module - 1) : 'K';

  return verifier === unformatted.slice(-1);

}

console.log(validate('14.211.109-8-')); // true, it should be false
console.log(validate('14.211.109-8 ')); // true, it should be false

Your code should be like:

const validate = (value) => {

    if (!/^.*[-\s]$/.test(value)) {
      return false;
    }

    const unformatted = unformat(value);


    let remainer = parseInt(unformatted.slice(0, -1), 10);
    let module = 1;
    let counter = 0;

    while (remainer > 0) {
      module = (module + (remainer % 10) * (9 - counter++ % 6)) % 11;
      remainer = Math.floor(remainer / 10);
    }

    const verifier = module > 0 ? '' + (module - 1) : 'K';

    return verifier === unformatted.slice(-1);

  }

  console.log(validate('14.211.109-8-')); // it should return false
  console.log(validate('14.211.109-8 ')); // it should return false
  console.log(validate('14.211.109-8')); // it should return true

If you don't want to allow a space at the start or a space or - at the end, you can use an alternation | with a start ^ and end $ anchor.

^ |[ -]$

If you want to match a whitespace char you can use \s but note that it can also match a newline.

^\s|[\s-]$

For example

if (/^ |[ -]$/.test(value)) {
    return false;
}

function unformat(value) {
  if (!value) {
    return '';
  }

  return value.replace(/^0+|[^0-9kK]+/g, '').toUpperCase();
}


function validate(value) {

  if (/^ |[ -]$/.test(value)) {
    return false;
  }

  const unformatted = unformat(value);

  if (/^0+/.test(unformatted)) {
    return false;
  }

  let remainer = parseInt(unformatted.slice(0, -1), 10);
  let module = 1;
  let counter = 0;

  while (remainer > 0) {
    module = (module + (remainer % 10) * (9 - counter++ % 6)) % 11;
    remainer = Math.floor(remainer / 10);
  }

  const verifier = module > 0 ? '' + (module - 1) : 'K';

  return verifier === unformatted.slice(-1);

}

console.log(validate('14.211.109-8-')); // false
console.log(validate('14.211.109-8 ')); // false
console.log(validate(' 14.211.109-8')); // false
console.log(validate('14.211.109-8')); // true

Related