如何仅在输入中显示逗号?
How to display a comma only in an input?
在输入HTML中,目前用户可以输入多个逗号,我想将逗号的个数减少1
。我不知道怎么做?
例如:用户不能输入20......01
只能输入20.01
app.component.html
<input notdot maxlength="5" [(ngModel)]="value" />
app.component.ts
export class AppComponent {
value
}
app.module.ts
@NgModule({
imports: [BrowserModule, FormsModule],
declarations: [AppComponent, NotDotDirective],
bootstrap: [AppComponent],
})
export class AppModule {}
dot.directive.ts
import { Directive,Injector,HostListener,Optional,Host } from '@angular/core';
import {NgControl} from '@angular/forms'
@Directive({
selector: '[notdot]'
})
export class NotDotDirective {
constructor(@Optional() @Host()private control:NgControl) {}
@HostListener('input', ['$event'])
change($event) {
const item = $event.target
const value = item.value;
const pos = item.selectionStart; //get the position of the cursor
const matchValue = value.replace(/,/g, '.')
if (matchValue!=value)
{
if (this.control)
this.control.control.setValue(matchValue, { emit: false });
item.value = matchValue;
item.selectionStart = item.selectionEnd = pos; //recover the position
}
}
}
这是一个复制品 -> here。如果你有解决方案,我真的很感兴趣。
非常感谢您的帮助。
根据您的指令,您可以将第 20 行更改为:
const matchValue = value.replace(/,/g, '.').replace(/\.\./, '.');
您正在用点替换逗号,如果您插入多个点,它只会用一个点替换输入
Menier,然后使用 (但你再次检查 value.replace(/,/g, '.');
。有些像
regExpr = new RegExp('^([1-9]\d*|0)(\.)?\d*$');
@HostListener('input', ['$event'])
change($event) {
let item = $event.target
let value = item.value;
let pos = item.selectionStart;
let matchvalue = value.replace(/,/g, '.');
let noMatch: boolean = (value && !(this.regExpr.test(matchvalue)));
if (noMatch) {
item.selectionStart = item.selectionEnd = pos - 1;
if (item.value.length < this._oldvalue.length && pos == 0)
pos = 2;
if (this.control)
this.control.control.setValue(this._oldvalue, { emit: false });
item.value = this._oldvalue;
item.selectionStart = item.selectionEnd = pos - 1;
}
else
{
//we check here if matchValue!=value
if (matchvalue!=value)
{
item.value = matchvalue;
item.selectionStart = item.selectionEnd = pos;
}
this._oldvalue = matchvalue;
}
}
在输入HTML中,目前用户可以输入多个逗号,我想将逗号的个数减少1
。我不知道怎么做?
例如:用户不能输入20......01
只能输入20.01
app.component.html
<input notdot maxlength="5" [(ngModel)]="value" />
app.component.ts
export class AppComponent {
value
}
app.module.ts
@NgModule({
imports: [BrowserModule, FormsModule],
declarations: [AppComponent, NotDotDirective],
bootstrap: [AppComponent],
})
export class AppModule {}
dot.directive.ts
import { Directive,Injector,HostListener,Optional,Host } from '@angular/core';
import {NgControl} from '@angular/forms'
@Directive({
selector: '[notdot]'
})
export class NotDotDirective {
constructor(@Optional() @Host()private control:NgControl) {}
@HostListener('input', ['$event'])
change($event) {
const item = $event.target
const value = item.value;
const pos = item.selectionStart; //get the position of the cursor
const matchValue = value.replace(/,/g, '.')
if (matchValue!=value)
{
if (this.control)
this.control.control.setValue(matchValue, { emit: false });
item.value = matchValue;
item.selectionStart = item.selectionEnd = pos; //recover the position
}
}
}
这是一个复制品 -> here。如果你有解决方案,我真的很感兴趣。
非常感谢您的帮助。
根据您的指令,您可以将第 20 行更改为:
const matchValue = value.replace(/,/g, '.').replace(/\.\./, '.');
您正在用点替换逗号,如果您插入多个点,它只会用一个点替换输入
Menier,然后使用 value.replace(/,/g, '.');
。有些像
regExpr = new RegExp('^([1-9]\d*|0)(\.)?\d*$');
@HostListener('input', ['$event'])
change($event) {
let item = $event.target
let value = item.value;
let pos = item.selectionStart;
let matchvalue = value.replace(/,/g, '.');
let noMatch: boolean = (value && !(this.regExpr.test(matchvalue)));
if (noMatch) {
item.selectionStart = item.selectionEnd = pos - 1;
if (item.value.length < this._oldvalue.length && pos == 0)
pos = 2;
if (this.control)
this.control.control.setValue(this._oldvalue, { emit: false });
item.value = this._oldvalue;
item.selectionStart = item.selectionEnd = pos - 1;
}
else
{
//we check here if matchValue!=value
if (matchvalue!=value)
{
item.value = matchvalue;
item.selectionStart = item.selectionEnd = pos;
}
this._oldvalue = matchvalue;
}
}