gpt4 book ai didi

javascript - 仅当输入数字时才 trim 最后一位数字。

转载 作者:行者123 更新时间:2023-12-03 04:32:07 24 4
gpt4 key购买 nike

     applicantAddressZipCode.subscribe(function (newValue) {
if (newValue != undefined && newValue != '') {

applicantAddressZipCode(formatZipCode(newValue));
var dataValue = String(applicantAddressZipCode().replace(/[^\/\d]/g, ''));
if (dataValue > 9999999999999.99 || dataValue < 0) {
applicantAddressZipCode('');
}

if (loading == false) {
sendCommand('SAVE');
}
}
});



function formatZipCode(value) {

value = value.replace(/[^\/\d]/g, '');
var z = /(\d{5})-?(\d{4})/
if (value != undefined && value != '') {
return value = value.replace(z, "$1-$2")
}


else {
return value;
}
};

我创建了一个用于邮政编码输入的函数。如果用户输入 12345-6789,则该功能可以正常工作,并且 12-3456789 也会得到 12345-6789。我的问题是,当用户仅输入诸如 1234567890 之类的数字时,该函数将转到 12345-67890。我想创建一个函数,仅在输入不带连字符的数字时 trim/切片最后一位数字。我将衷心感谢您的帮助!

最佳答案

  1. 首先您需要检查该数字不包含连字符。使用这行代码来检查这一点。如果输入数字中没有连字符,则连字符的索引将为-1。

    if(inputValue.indexOf('-') == -1){
    //input do not contain hyphen so remove the last character
    }
  2. 然后您需要调用另一个函数或代码,将您收到的输出中的最后一个字符 trim 为 12345-67890。

    tempOutput.slice(0,-1);
    //where tempOutput contains the temporary output 12345-67890

关于javascript - 仅当输入数字时才 trim 最后一位数字。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43446005/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com