开发者

HTML text input limitation

开发者 https://www.devze.com 2023-01-23 02:58 出处:网络
I want to ask a question about the HTML code. I have the following UI to let the users to input the data.

I want to ask a question about the HTML code. I have the following UI to let the users to input the data.

  1. text box
  2. button

the following is the code:

<input type="text" id="userid" name="userid" value="Please enter your user ID" />
<input type="button" id="enterButton" name="enterButton" value="Enter" />

I have implemented the auto-complete. It is like you search the google, it gives you a suggestion list. And my suggestion list is the following:

Sam
Tom
Chris

My question is that is it possible to limit the user enter in the text?

Just like, the use开发者_JS百科r can only enter the Sam, Tom and Chris. The users do not allow to enter the Samxx and Tay, etc. thank you.

P.S: I must use the input type="text", I cannot use the dropdown list.


Your question of limiting user to type in only limited number of names is possible only if you have a set of predefined names. If this is the case then there is no need for a Inoutbox you can have a dropdown.

Why not try to solve the issue with another approach?

Instead of fetching names which are similar to the entered values try fetching names which are like the values entered. For example in your case if the user types in Samxx you can show Sam. i.e Matching your name collection with the textbox values.


Once the form is submitted, test the value of the text field with javascript. E.g.:

if (document.getElementById("userid").value==="Sam"||document.getElementById("userid").value==="Tom"||document.getElementById("userid").value==="Chris"){
//Code to accept data

}

else{

//Do nothing or alert the user that a different value is expected

}
0

精彩评论

暂无评论...
验证码 换一张
取 消