簡體   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