当在文本框中输入任何新的新值时,光标位置正在改变

Cursor position is changing while any new new value entered in a textbox

本文关键字:光标 位置 改变 新值时 任何新 文本 输入      更新时间:2023-09-26

定义:我有一个最大长度为11个字符的文本框。如果我尝试在文本框的中间输入值,光标位置将自动移动到结束位置。你能帮我把光标的位置设置好吗?

注意:函数用于只允许字母数字值的文本框。

这里的代码

<!DOCTYPE html>
<html>
<body>
<script>
//function for alphanumberic validation
function alphaNumericOnly(i){
if(i.value != null) 
    {
        i.value = i.value.replace(/[^'w]+/g, ''); 
    }
    }
</script>
<input type="text" maxlength="11" id="policyOrBondNum" onkeyup="alphaNumericOnly(this);" />
</body>
</html>

Try-

<input type="text" maxlength="11" id="policyOrBondNum" onkeyup="alphaNumericOnly(this);" style="text-align: right; padding-right: 50px;"/>