js 退格键并计算有多少个数字

发布于 2024-09-10 04:47:10 字数 787 浏览 6 评论 0原文

我有这个代码用于计算输入的数字数

var tnnod=0;
function telephone(e) {
    if (tnnod<10) {
        var key;
        var keychar;

        if (window.event) {
            key = window.event.keyCode;
        }
        else if (e) {
            key = e.which;
        }
        else {
            return true;
        }
        keychar = String.fromCharCode(key);

if ((key==null) || (key==0) || (key==8) ||  (key==9) || (key==13) || (key==27) ) {
   return true;
}
else if ((("0123456789").indexOf(keychar) > -1)) {
    tnnod+=1;
   return true;
}
else if (keychar == "-") { 
  return true;
}
else
   return false;
}
    else
        return false
}

,但是每次按退格键时如何从计数器中删除 1 并且删除的字符是数字而不是“-”

我尝试过将 key == 8 获取到做某事,但由于某种原因按退格键并没有真正返回任何内容,

这可能是什么问题?

i have this code for counting how many digits where entered

var tnnod=0;
function telephone(e) {
    if (tnnod<10) {
        var key;
        var keychar;

        if (window.event) {
            key = window.event.keyCode;
        }
        else if (e) {
            key = e.which;
        }
        else {
            return true;
        }
        keychar = String.fromCharCode(key);

if ((key==null) || (key==0) || (key==8) ||  (key==9) || (key==13) || (key==27) ) {
   return true;
}
else if ((("0123456789").indexOf(keychar) > -1)) {
    tnnod+=1;
   return true;
}
else if (keychar == "-") { 
  return true;
}
else
   return false;
}
    else
        return false
}

but how do i remove 1 from the counter each time the backspace was hitted and the char that was deleted was a digit and not "-"

i have tried getting the key == 8 to do something but hitting the backspace doesn't really return anything for some reason

what can be the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

九歌凝 2024-09-17 04:47:10

您不必专门检测退格键。试试这个:

var tn_count = 0;
function telephone(ev) {
  var el = document.getElementById("telephone_number");

  if(tn_count < 10) {
    var key, keychar;
    if(window.event) {
      key = window.event.keyCode;
    } else {
      key = ev.which;
    }

    keychar = String.fromCharCode(key);
  }

  if(!keychar.match(/\d|-/)) {  // only allow digits or "-"
    return false;
  }

  // clean up any non-digit chars that get in here somehow
  el.value = el.value.replace(/[A-Za-z]+/, '');
  tn_count = el.value.replace("-",'').length; // get the digit length
  return true;
}

这里的基本区别在于,不是每次按键时加 1,而是将 tn_count 更新为字段中所有数字字符的总数。为了安全起见,您可能可以进行更多清理,但这应该可以帮助您开始。

You don't have to detect specifically the backspace keypress. Try this:

var tn_count = 0;
function telephone(ev) {
  var el = document.getElementById("telephone_number");

  if(tn_count < 10) {
    var key, keychar;
    if(window.event) {
      key = window.event.keyCode;
    } else {
      key = ev.which;
    }

    keychar = String.fromCharCode(key);
  }

  if(!keychar.match(/\d|-/)) {  // only allow digits or "-"
    return false;
  }

  // clean up any non-digit chars that get in here somehow
  el.value = el.value.replace(/[A-Za-z]+/, '');
  tn_count = el.value.replace("-",'').length; // get the digit length
  return true;
}

The basic difference here is that instead of adding 1 every time the key is pressed, just updated tn_count to be the total count of all digit characters in the field. You can probably do some more cleanup just to be safe, but this should get you started.

信仰 2024-09-17 04:47:10

我认为为类似的事情计算击键次数是一个坏主意。我们正在讨论文本字段的输入,对吧?如果用户从剪贴板粘贴某些字符串,您会怎么做?如果他用鼠标标记一些文本并将其删除怎么办?用一个字符替换它?

我认为仅查看文本字段中的文本并(如有必要)进行一些摆弄以确保语法正确会更有意义。让用户输入他想要的任何内容,如果您发现垃圾字符,您可以将文本替换为不包含这些字符的文本。此时您还可以修剪字段(没有前导或尾随空格)。此外,准确跟踪长度变得就像询问从字段返回的字符串的长度一样简单。

I think it's a bad idea to count keystrokes for something like that. We're talking about input into a text field, right? What will you do if the user does a paste of some string from the clipboard? What if he uses the mouse to mark some text and delete it? Replace it with one character?

I think it would make a lot more sense to just look at the text from the text field and (if necessary) do some fiddling to ensure proper syntax. Let the user enter whatever he wants, if you find garbage characters you can just replace the text with one that doesn't have those characters. You can also trim the field at this time (no leading or trailing spaces). Also, keeping accurate track of the length becomes as easy as asking for the length of the string returned from the field.

暗恋未遂 2024-09-17 04:47:10

一些想法 -

  1. 是否可以使用 3 个字段而不是 1 个?然后您可以稍后添加破折号。
  2. 如果您想使用当前的方法,您可以保留已输入的破折号的计数器。然后,在每次击键时,检查还剩下多少个破折号。如果与之前的计数不同,您就知道他们已经删除了破折号。
  3. 我认为它需要更强大一点。如果他们在字符串中的奇怪位置添加破折号怎么办?
  4. 您还可以阻止用户输入所有非数字字符并在每个分隔点插入破折号。因此,在输入 3 和 6 数字后插入破折号。

A few thoughts -

  1. Would it be possible to use 3 fields instead of 1? Then you could add the dashes later.
  2. If you want to use your current method, you might keep a counter of the dashes that have been typed. Then, on each key stroke, check to see how many dashes are left. If it's different than the previous count, you know they've deleted the dashes.
  3. I think it needs to be a bit more robust. What if they put a dash in an odd place within the string?
  4. You could also prevent the user from entering all non-numeric characters and insert the dashes at each point of separation. So, insert a dash after 3 and 6 numbers as they are typing.
违心° 2024-09-17 04:47:10

您可以计算字符串的长度并使用该值吗?像下面这样:

function getLen(el) {
    return el.value.replace('-', '').length;
}

alert(getLen(document.getElementById('telephone_number')));

Could you just count the length of the string and use that value? Something like the following:

function getLen(el) {
    return el.value.replace('-', '').length;
}

alert(getLen(document.getElementById('telephone_number')));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文