I have to define a textbox in html whose maximum size should be 3.
Now If I am entring -100 then these are 4 characters but I want that user should allow to enter 100 or -100. If I define size="3" then it will not allow -100. So I want to know can we 开发者_StackOverflowdefine size of the textbox dynamically,i.e. if I enter "-" sign then size will increase to 4 else it should be 3.Can we do this using js?
Call this function on the key down event of the text box
function check(e){
var unicode=e.charCode? e.charCode : e.keyCode
if(if unicode==109){
//increase the size of text box to 4;
}
}
You could use regular expressions in a keydown event: If the regexp doesn't match, the function returns FALSE.
Possible regular expression:
^[-]{0,1}\d{0,3}$
精彩评论