/^ + {0,1}(?:\ d \ s?){11,13} $ /この正規表現では、キー入力のregexを使ってテキストボックスの入力を制限する
私は、ユーザーが唯一のpart..why正規表現は、このシナリオで
function ValidatePhone(phone) {
var expr = /^\+?(?:\d\s?){11,13}$/;
return expr.test(phone);
}
var countofPlus = 0;
$("#phone").on("keypress", function (evt) {
if (evt.key == "+")
{
countofPlus = countofPlus + 1;
if (countofPlus > 1 || this.value.length >= 1) {
return false;
}
else return true;
}
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && charCode != 43 && charCode != 32 && charCode != 40 && charCode != 41 && (charCode < 48 || charCode > 57))
return false;
return true;
});
$("#phone").on("keyup", function (evt) {
debugger;
if (evt.key == "+") {
countofPlus--;
return true;
}
});
https://stackoverflow.com/a/37421357/1647737 –
これはおそらく、ユーザーが1つのキーを押すだけで11文字の必要最小限を入力することができないために機能しません。 –
これは本当に奇妙で非ユーザーですフレンドリーな実装です。 ** 'keypress'関数**を使わないでください。たとえば、フォーカスがページ要素を離れるときに、数値が予期しない形式である場合は、テキストボックスに*警告*を表示できます。 (そして、バックエンドでもう一度検証してください) –