1. Control the number of characters entered by the user
For single-line text boxes and password input boxes, the maxlength attribute can be used to control the number of characters entered by the user.
For multi-line text, maxlength is a custom property with the largest number of characters input. When the onkeypress event occurs, the return value of the LessThan() function is returned. The function is as follows
<textarea name="comments" id="comments" cols="40" rows="4" maxlength="50" onekeypress ="return LessThan(this);"></textarea>
Detailed code
The code copy is as follows:
<script language="javascript">
function LessThan(oTextArea){
//Return the boolean value required by the number of characters in the text box.
return oTextArea.value.length < oTextArea.getAttribute("maxlength");
}
</script>
<form method="post" name="myForm1" action="addInfo.aspx">
<p><label for="name">Please enter your name:</label>
<input type="text" name="name" id="name" value="name" maxlength="10"></p>
<p><label for="comments">I want to leave a message:</label><br>
<textarea name="comments" id="comments" cols="40" rows="4" maxlength="50" onkeypress="return LessThan(this);"></textarea></p>
<p><input type="submit" name="btnSubmit" id="btnSubmit" value="Submit">
<input type="reset" name="btnReset" id="btnReset" value="Reset"></p>
</form>
2. Set the mouse to automatically select text
First, the mouse automatically focuses onmouseover = "this.focus"
Next is onfocus = "this.select()"
Code example:
The code copy is as follows:
<form method="post" name="form1" id="form1" action="addInfo.aspx">
<input type="text" name="name" id="name" value="name" onmouseover="this.focus()" onfocus="this.select()">
</form>
For multiple code instances, you can use the following code to focus
The code copy is as follows:
<script type="text/javascript">
function myFocus() {
this.focus();
}
function mySelect() {
this.select();
}
window.onload = function() {
var oForm = document.forms["myForm1"];
oForm.name.onmouseover = myFocus;
oForm.name.onfocus = mySelect;
}
</script>
<form method="post" name="myForm1" action="addInfo.aspx">
<p>
<label for="name">Please enter your name:</label>
<input type="text" name="name" id="name" value="name">
</p>
<p>
<label for="passwd">Please enter your password:</label>
<input type="password" name="passwd" id="passwd">
</p>
<p>
<input type="submit" name="btnSubmit" id="btnSubmit" value="Submit">
<input type="reset" name="btnReset" id="btnReset" value="Reset">
</p>
</form>