我試圖構建一個正則表達式排除kommas和點在輸入(僅數字)。我在一個運行Angular2的Ionic 2應用程序中這樣做,所以我使用了Angular 2 Validators.pattern。 我會假裝正則表達式引擎是JavaScript的一個。正則表達式排除komma和點
我看了一些帖子的正則表達式,並喜歡它,如果你輸入1,2
但它不工作1,
觸發建立這種模式[^,.]+
這工作得很好。
1,
仍然有效,但我希望它是無效的,就像1,2。那麼我該如何修改我的模式才能獲取x,x
,x.x
也作爲x,
和x.
?
很抱歉,如果這個問題有點愚蠢的,但我從來沒有使用正則表達式模式befor並試圖爲3或4個小時,我投降後...
將是巨大的,如果你們中的一些可以幫助我得到這個正則表達式加工。
編輯:
在我的TS文件
this._formValidator = new FormGroup({
inputValidator: new FormControl('', Validators.maxLength(this._maxLength)),
inputValidatorInteger: new FormControl('', Validators.compose([
Validators.pattern('[^,.]+')
]))
});
在Valuechange我映射錯誤到正確的錯誤 - 消息並顯示出來。也嘗試登錄任何錯誤,但沒有,如果我輸入1,
。如果我輸入類似1,2
之類的東西,即使1,,,,,
無效,也是無效的。 我需要使用:當我與例如不含1,而不是exluding,其作品像它應該,[^1]+
在我的HTML文件
<ion-item class="listElement" [formGroup]="_formValidator">
<ion-input [(ngModel)]="_result.value" type="number" placeholder="Enter Something" formControlName="inputValidatorInteger"></ion-input>
</ion-item>
EDIT 2嘗一嘗。那是因爲我需要數字鍵盤才能在智能手機上打開,並且type =「number」。 正則表達式似乎不適用於type =「number」,或者說它不能很好地工作。
只是測試 - 加上'^'和'$'錨:'^[^,。] + $'' –
你的意思是*抓取'x,x','xx'也可以作爲'x,'和'x.' * –
你的模式應該可以工作,看[這裏]( http://plnkr.co/edit/VKuGMI33OkSmvqnEqnCU?p=preview)。 –