繁体   English   中英

Javascript 验证字符串正则表达式

[英]Javascript validate string regex

我有这个验证功能:

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/

当现在变得true时,这个想法在两种情况下都是false的:

  1. 当字符串以 ``//- 结尾时
  2. 当字符串结束时 // 空格处

我该如何修改验证 function?

您可以在 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

你的代码应该是这样的:

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

如果您不想在开头或结尾处允许空格或 - ,则可以使用替换| 带有开始^和结束$锚。

^ |[ -]$

如果要匹配空白字符,可以使用\s但请注意它也可以匹配换行符。

^\s|[\s-]$

例如

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM