我是JavaScript的新手,我想添加到我的输入文本,IBAN帐户注册的空间插入.
<input type="text" name="iban" onkeyup="if(this.value.length > 34){this.value=this.value.substr(0, 34);}" />Run Code Online (Sandbox Code Playgroud)
有我的输入字段; 谁能告诉我怎么做到这一点?
当用户使用JavaScript键入信用卡号时,我需要在每四个字符后插入一个空格.
<input type="text" size="19" maxlength="19" data-stripe="number" placeholder="1234 5678 9012 3456">
Run Code Online (Sandbox Code Playgroud)
完成此任务的最佳方法是什么?
当您输入公式时,我试图在我的应用程序中重现类似于 Microsoft Excel / Google Sheets 的用户体验,并且您可以使用不同的公式和变量来自动完成下拉菜单。
为此,在验证自动完成功能后,我希望能够控制光标的位置。
例如,如果我输入=sum(变量1,变量2),则在变量2自动完成时,光标应该位于最后一个括号之前,而不是在最后。
我了解如何使用javascript设置光标的位置,问题是因为我同时修改输入的值并设置光标位置,后者不起作用。
我用更简单的上下文重现了问题: https ://jsfiddle.net/joparisot/j8ourfa1/31/
我的HTML:
<div id="app">
<autocomplete v-model="selection"></autocomplete>
</div>
<template id="autocomplete">
<div>
<h2>gernerogrnio</h2>
<input id="my-input"
class="form-control"
type="text"
:value="value"
@keydown.enter="updateValue($event.target.value)">
<p>{{ value }}</p>
</div>
</template>
Run Code Online (Sandbox Code Playgroud)
我的脚本:
Vue.component('autocomplete', {
template: '#autocomplete',
props: {
value: {
type: String,
required: true
}
},
methods: {
updateValue (value) {
var new_value = ''
if (value.length < 4) {
new_value = 'Inferior'
} else {
new_value = 'Superior'
}
this.$emit('input', new_value)
var myInput = document.getElementById('my-input');
this.setCaretPosition(myInput, …Run Code Online (Sandbox Code Playgroud) 我正在尝试在输入字段中每 4 个数字后添加一个空格。
这是我的代码:
credit: function(e) {
const val = e.target.value;
const valArray = val.split(' ').join('').split('');
if(isNaN(valArray.join('')))
return;
if(valArray.length === 17)
return
if(valArray.length % 4 === 0) {
this.setState({ number: e.target.value + " " });
}else{
this.setState({ number: e.target.value})
}
},
Run Code Online (Sandbox Code Playgroud)
规则如下:用户只能写入numbers,长度应为 16,并在每 4 个数字后添加空格。
问题是 :
1:在数字末尾有一个额外的空格,添加在最后一个数字之后
2:我无法使用backspace删除元素(推回空格后,它将在数字前面添加空格)
我需要在输入的每4位数字后添加一个空格,我要在控制台中获取它,我如何才能实现这一点来更改角度5的输入
我在以下.ts中使用的代码
mychange(val) {
const self = this;
let chIbn = val.split(' ').join('');
if (chIbn.length > 0) {
chIbn = chIbn.match(new RegExp('.{1,4}', 'g')).join(' ');
}
console.log(chIbn);
this.id = chIbn;
}
Run Code Online (Sandbox Code Playgroud)
html
<input class="customerno" (ngModelChange)="mychange($event)" [formControl]="inputFormControl" (keydown.backspace)="onKeydown($event)" maxlength="{{digit}}" (keyup)="RestrictNumber($event)" type="tel" matInput [(ngModel)]="id" placeholder="Customer No. ">
Run Code Online (Sandbox Code Playgroud)
安慰
1
11
111
1111
1111 1
1111 11
1111 111
1111 1111
1111 1111 1
1111 1111 11
1111 1111 111
1111 1111 1111
Run Code Online (Sandbox Code Playgroud)
注意:我从Angular 2改编而成:输入,卡号输入中每4位数字后添加连字符。但是我用空间改变了hypen