2016-04-27 64 views
0

鏈接到整個項目(不nodes_modules) https://www.dropbox.com/s/4qd8x97ih1f1nho/workinghw4zipped%20%281%29.zip?dl=0Angular2的Javascript驗證輸入數字而不是字符

注意:一旦你運行它,關於這個問題的一切是網站的「編輯所有」部分中,按一下訪問表格

這部分網站的目的是讓用戶輸入表格中每一行的名稱和價格。

如果用戶在價格框內輸入一個字符(不是數字),那麼必須有一個「無效輸入」消息,在用戶輸入時更新自己。

我應該在代碼中添加或更改哪些內容,以便在價格框中輸入字符(而非數字)時允許用戶輸入「無效輸入」消息?

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; } 
} 
+0

輸入類型使用isNaN函數是一個數字,它仍然接受字符找到ISNUMBER? –

+0

「input type =」number「」應該「阻止」用戶輸入字符,但我仍然想要一種方式向用戶發送消息,以防他們在價格字段中輸入字符 – erin

回答

0

我不知道這是否會工作。但這是我解決您的問題的方法。我希望它對你有用。

更換您的輸入控制價格與本 -

<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!'; 
} 
} 
+0

實施後並運行你的代碼我得到了這些錯誤(截圖): https://www.dropbox.com/s/e71222zvkrmv6qx/Screen%20Shot%202016-04-27%20at%2012.26.44%20PM.png?dl=0 這個下一個截圖是它應該看起來像什麼,除了「這不是一個數字!」消息將取代「輸入原始」 https://www.dropbox.com/s/gsg2rmjligcsgyw/Screen%20Shot%202016-04-27%20at%2012.32.24%20PM.png?dl= 0 – erin

+0

用{{errorMessage}}替換輸入原始碼,並確保您的html標記正確打開和關閉(以消除模板分析錯誤) –

+0

我認爲我們正在接近,但現在我得到一個不同的錯誤(見截圖) https://www.dropbox.com/s/mlk9sz8augjcrko/Screen%20Shot%202016-04-27%20at%2012.57.46%20PM.png?dl=0 – erin

0

可以通過在價格管理角2.

checkData(username: string, email: string, phone: string, msg: string) { 
    if (isNaN(Number(phone))) { 
     //code 
    } else { 

    } 
    }