Limit number of characters allowed in form input t

2019-01-02 20:48发布

How do I limit or restrict the user to only enter a maximum of five characters in the textbox?

Below is the input field as part of my form:

<input type="text" id="sessionNo" name="sessionNum" />

Is it using something like maxSize or something like that?

9条回答
情到深处是孤独
2楼-- · 2019-01-02 21:36

Add the following to the header:

<script language="javascript" type="text/javascript">
function limitText(limitField, limitNum) {
    if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);
    }
}
</script>

    <input type="text" id="sessionNo" name="sessionNum" onKeyDown="limitText(this,5);" 
onKeyUp="limitText(this,5);"" />
查看更多
伤终究还是伤i
3楼-- · 2019-01-02 21:38

The simplest way to do so:

maxlength="5"

So.. Adding this attribute to your control:

<input type="text" 
    id="sessionNo" 
    name="sessionNum" 
    onkeypress="return isNumberKey(event)" 
    maxlength="5" />
查看更多
弹指情弦暗扣
4楼-- · 2019-01-02 21:42
<input type="text" maxlength="5">

the maximum amount of letters that can be in the input is 5.

查看更多
登录 后发表回答