简体   繁体   中英

Allow only numbers with only specific alphabets in textbox

i have a text box in html, i want to allow user to only input following range

0-9 and NA

there are two cases
1: user inputs range form 0-9 numbers and
2: user inputs only NA (NA denotes Not applicable)

how could i allow user to do this

I try following code but it does not work

<input type = "number" autocomplete="off" class = "form-control"  name="Personal_Weapons_Price" id = "Personal_Weapons_Price"   required  onkeyup="this.value = this.value.toUpperCase();" pattern="[^0-9NA]+" />

您可以采用以下模式,它查找一个或多个数字或NA

^(\d+|NA)$

添加这种方式oninput="this.value = this.value.toUpperCase().replace(/[^NA0-9]/, '')"

 <input type="text" oninput="this.value = this.value.toUpperCase().replace(/[^NA0-9]/, '')" />

The correct pattern is

pattern="^(\d+|NA)$"

If you also want to match na (so you can remove the onkeyup listener):

pattern="^(\d+|NA|na)$"

Edit : if you want to write "NA", you should change the type attribute type from number to text .

<input 
    type="text" 
    autocomplete="off" 
    class="form-control" 
    name="Personal_Weapons_Price" 
    id="Personal_Weapons_Price" 
    required 
    pattern="^(\d+|NA|na)$" 
/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM