gpt4 book ai didi

javascript - 验证只有 5 个值的文本字段

转载 作者:行者123 更新时间:2023-12-02 19:11:57 26 4
gpt4 key购买 nike

 <label class="user-reg-label fl">Percentage:<span class="required">*</span></label>
<input class="user-reg-input fl" type="text" name="Percentage[]" id="Percentage1" value="" maxlength="5" />

this is my html code. i need to validate this input field with javascript to enter only 5 values like 99.99

最佳答案

这是格式化,而不是验证,但这似乎是OP真正寻找的(我认为)。

var input = document.getElementById("Percentage1"),
onlyNumbers = input.value.replace(/\D/g,""), // Remove all nondigits
cleanNumbers = onlyNumbers.substr( 0, 4 ); // Limit to four digits

// Add decimal if more than two digits in length
if( cleanNumbers.length > 2 )
{
cleanNumbers = cleanNumbers.substr( 0, 2 ) + "." + cleanNumbers.substr( 2 );
}

input.value = cleanNumbers;

关于javascript - 验证只有 5 个值的文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13598023/

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