本文介绍了如何为IBAN注册每4个字符插入空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我是JavaScript的新手,我想在我的输入文本中添加IBAN帐户注册的空格插入。 < input type =textname =ibanonkeyup =if(this.value.length> 34){this.value = this.value.substr (0,34);}/> 有我的输入字段;有人能告诉我怎么做吗?解决方案现有的答案相对较长,看起来像是过度杀戮。另外,它们不能完全发挥作用(例如,一个问题是您无法编辑以前的字符)。 对于那些感兴趣的人,根据维基百科:这是一个相对较短的版本,类似于现有的答案: document.getElementById('iban')。addEventListener('input',function(e){e.target.value = e .target.value.replace(/ [^ \ dA-Z] / g,'')。renplace(/(。{4})/ g,'$ 1')。trim();}); < label for =iban> iban< / label> < input id =ibanty pe =textname =iban/> 如上所述,需要注意的是,您无法返回并编辑以前的字符。如果你想解决这个问题,你需要通过最初访问 selectionEnd 属性然后在之后设置插入符号位置来检索插入符号的当前位置。已应用正则表达式格式。 document.getElementById('iban')。addEventListener('input',function(e){var target = e.target,position = target.selectionEnd,length = target.value.length; target.value = target.value.replace(/ [^ \dA-Z] / g,'').replace(/(。{ 4})/ g,'$ 1')。trim(); target.selectionEnd = position + =((target.value.charAt(position - 1)===''&& target.value.charAt(length - 1)===''&& length!== target.value.length)?1:0);}); < label for =iban> iban< / label>< input id =ibantype =textname =iban/> 当插入符号后面的字符是一个空格时,你会发现存在一个小问题(因为在最初检索插入符号的位置时没有计算空间)。要解决此问题,如果后续字符是空格(假设实际添加了空格 - 通过比较替换字符之前和之后的长度来确定),则手动递增位置。 I'm really new in JavaScript and I would like to add to my input text, space insertion for IBAN account registering.<input type="text" name="iban" onkeyup="if(this.value.length > 34){this.value=this.value.substr(0, 34);}" />There is my input field; could someone tell me how I can do this? 解决方案 The existing answers are relatively long, and they look like over-kill. Plus they don't work completely (for instance, one issue is that you can't edit previous characters).For those interested, according to Wikipedia:Here is a relatively short version that is similar to the existing answers:document.getElementById('iban').addEventListener('input', function (e) { e.target.value = e.target.value.replace(/[^\dA-Z]/g, '').replace(/(.{4})/g, '$1 ').trim();});<label for="iban">iban</label><input id="iban" type="text" name="iban" />As stated above, the caveat is that you can't go back and edit previous characters. If you want to fix this, you would need to retrieve the caret's current position by initially accessing the selectionEnd property and then setting the caret's position after the regex formatting has been applied.document.getElementById('iban').addEventListener('input', function (e) { var target = e.target, position = target.selectionEnd, length = target.value.length; target.value = target.value.replace(/[^\dA-Z]/g, '').replace(/(.{4})/g, '$1 ').trim(); target.selectionEnd = position += ((target.value.charAt(position - 1) === ' ' && target.value.charAt(length - 1) === ' ' && length !== target.value.length) ? 1 : 0);});<label for="iban">iban</label><input id="iban" type="text" name="iban" />You will notice that there is a slight issue when the character after the caret is a space (because the space wasn't accounted for when initially retrieving the caret's position to begin with). To fix this, the position is manually incremented if the succeeding character is a space (assuming a space was actually added - which is determined by comparing the length before and after replacing the characters). 这篇关于如何为IBAN注册每4个字符插入空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-19 22:59