![](/img/trans.png)
[英]I want to Create textbox which allows only numbers. How can I do that with Typescript in aurelia?
[英]I want to automatically add dashes (-) in a textbox. How can I do this in an aurelia typescript application?
在這里,我正在嘗試創建一個文本框,該文本框將允許 XXX-XXX-XXXX 格式的聯系號碼。 但我無法創建這個。 如何在 typescript aurelia 中創建格式化的聯系號碼?
<input type="text" name.bind="name" class="form-control" placeholder.bind="placeholder" maxlength.bind="maxlength" onblur="addHyphen(f)"/>
addHyphen(f){
f.value = f.value.slice(0,3)+"-"+f.value.slice(3,6)+"-"+f.value.slice(6,10);
}
我對此進行了更新,使其類似於您在評論中提供的 JsFiddle/
<template>
<input type="text" name.bind="name" class="form-control"
placeholder.bind="placeholder" maxlength.bind="maxlength
keydown.delegate="keydown($event)" ref="text"/>
</template>
export class Textbox {
public text: HTMLInputElement;
public keydown(e) {
const key = e.key || e.keyCode || 0;
if (key !== 8 && key !== 9) {
if (this.text.value.length === 3) {
this.text.value += '-';
console.log(e);
}
if (this.text.value.length === 7) {
this.text.value += '-';
console.log(e);
}
}
return (key == 8 || key == 9 || key == 46 || (key >= 48 && key <= 57) || (key >= 96 && key <= 105));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.