![](/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.