This article describes how to use the Length attribute of a javascript string to limit the number of input texts in a text box.
In this example, The Length attribute of the JS string is used to limit the length of the text in a text box. The general idea is as follows: Whenever a user enters a value in the text box (Keyboard hitting event), a function named check is triggered, and the length of Characters in the text box is obtained, check whether the length is between 5-10. If not, the corresponding warning is given. When the number of characters entered exceeds 10, the system automatically removes the longer part.
<SCRIPT type =" Text/JavaScript "> Function Check (){ VaR STR = Document . Getelementbyid (" Test "). Value; If (Str. Length <5) {Update (" Enter at least 5 characters! ");} Else If (Str. Length > 10) {Update (" It cannot exceed 10 characters! "); STR = Str. substring (0, 10 ); Document . Getelementbyid (" Test "). Value = Str. substring (0, 10 );} Else {Update (" Valid username. ")}} Function Update (Word ){ Document . Getelementbyid (" Feedback "). Innerhtml = word ;}</SCRIPT>
for = " test "> account: text " name = " test " id = " test " onkeypress = " check () " maxlength = " 15 "/> feedback ">