Angular2 Javascript 验证 输入数字而不是字符
Angular2 Javascript Validating Input number not character
链接到完整项目(不带nodes_modules)https://www.dropbox.com/s/4qd8x97ih1f1nho/workinghw4zipped%20%281%29.zip?dl=0
注意:运行它后,有关此问题的所有内容都在网站的"全部编辑"部分中,单击该部分以访问该表
网站这一部分的目的是让用户为表中的每一行输入名称和价格。
如果用户在价格框中输入字符(而不是数字),则必须在某处显示"无效输入"消息,该消息会在用户键入时自行更新。
我应该在代码中添加或更改什么,以允许用户在价格框中输入字符(而不是数字)时显示"无效输入"消息?
该 html 文件:
<h1>{{title}}</h1>
<h2>My Heroes</h2>
<form [ngFormModel]="myForm" class="ui form">
<table *ngFor="#hero of heroes">
<thead>
<tr>
<th> ID </th>
<th> Hero Name</th>
<th> Price </th>
</tr>
</thead>
<tbody>
<tr>
<td id="id"><label>{{hero.id}}</label></td>
<td id="name"><input type = "String" [(ngModel)]="hero.name" placeholder="Pristine name" /></td>
<div class="field">
<td id="price">
<input type = "number"
[(ngModel)]="hero.price"
placeholder="Pristine Price"
[ngFormControl]="myForm.find('price')"
/>Input Pristine</td>
</div>
<div *ngIf="!sku.control.valid"
class="ui error message ">Price can only be an integer.</div>
<div *ngIf="sku.control.hasError()"
class="ui error message">Input is invalid</div>
<div *ngIf="!myForm.valid"
class="ui error message">Not pristine.</div>
</tr>
</tbody>
</table>
</form>
.ts 文件:
import {Component, OnInit} from 'angular2/core';
import {
FORM_DIRECTIVES,
FormBuilder,
Control, ControlGroup,
Validators
} from 'angular2/common';
import {Router} from 'angular2/router';
import {Hero} from './hero';
import {HeroService} from './hero.service';
@Component({
selector: 'SpreadSheetComponent',
templateUrl: 'app/spreadsheeteditall.component.html',
styleUrls: ['app/spreadsheeteditall.component.css'],
providers: [HeroService],
})
export class SpreadSheetComponent {
myForm: ControlGroup;
price;
onSubmit(value: string): void {
console.log('you submitted value: ', value);
}
heroes: Hero[];
selectedHero: Hero;
constructor(fb: FormBuilder, private _heroService: HeroService, private _router: Router) {
function skuValidator(control: Control) : { [s: string]: boolean } {
if (!control.value.match(/[0-9][0-9]*('.[0-9][0-9])?$/)) {
return { invalidSku: true };
}
};
this.myForm = fb.group({
'sku': ['', Validators.compose([
Validators.required, skuValidator])]
});
this.price = this.myForm.controls['sku'];
}
getHeroes() {
this._heroService.getHeroes().then(heroes => this.heroes = heroes);
}
gotoDetail() {
this._router.navigate(['HeroDetail', { id: this.selectedHero.id }]);
}
ngOnInit() {
this.getHeroes();
}
onSelect(hero: Hero) { this.selectedHero = hero; }
}
我不确定这是否有效。但这是我解决你的问题的方法。我希望它对你有用。
将价格的输入控制替换为此 -
<input type = "number"
[(ngModel)]="hero.price"
placeholder="Pristine Price"
[ngFormControl]="myForm.find('price')"
(keypress)="showErrorIfNotANumber($event)"
/>
然后添加此标签以在您想要的任何位置显示消息 -
<p style="color:red;" *ngIf="errorMessage">{{errorMessage}}</p>
修改您的 component.ts 文件 -
1) 添加一个变量 -
public errorMessage:any = null;
2) 添加此功能 -
public showErrorIfNotANumber(event:any){
if(event.charCode>=48 && event.charCode<=57){
this.errorMessage = null;
}
else{
this.errorMessage = 'you can only enter numbers here!';
}
}
您可以通过在角度 2 中使用 isNaN 函数找到 isNumber。
checkData(username: string, email: string, phone: string, msg: string) {
if (isNaN(Number(phone))) {
//code
} else {
}
}
相关文章:
- 如何为jQuery屏蔽输入插件创建一个允许字母数字、空格和重音字符的掩码
- 带静态字符e输入的文本框数字和带javascript的负整数
- 使用正则表达式匹配长度为六个字符的字母数字字符串
- 删除图形和数字之间的连字符(-)符号
- 如何限制一个字段只接受4个数字字符作为输入,并获得'It’这是潜水警报
- 为什么这个正则表达式不't匹配最后一个字母数字字符
- 一个jquery验证器方法,它不接受纯数字或纯特殊字符,但接受上面是否有字母
- node.js可以识别字符模式,但不能识别数字模式
- 如何使用至少一个数字、一个大写字母和6-20个字符验证密码
- Javascript Regex选择每个非字母数字字符和空白
- Javascript中的正则表达式,用于只有数字的字符串,并且不应在除数字以外的任何位置包含任何字符
- JavaScript正则表达式(带_-的字母数字字符)
- 忽略任何非小写字符(如数字、大写字母和符号)并获取k的值
- 为什么Javascript中的整数加空格加字符串会导致数字和字符串的总和?(2++'22'=24)
- 在粘贴事件Javascript的输入字段中删除所有非数字字符
- JavaScript-Regex删除代码/特殊字符/数字等
- JavaScript:仅按字符或按字符+数字(如果存在)拆分字符串
- jQuery中的“#”字符(数字符号)是什么意思?
- 正则表达式,用于替换任何字符(数字/、.除外)
- jQuery tokeninput插件,当返回1字符数字的结果时出现问题