Arvind Thakur
Arvind Thakur

Reputation: 347

Dyanamically set size of textbox using js

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 define 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?

Upvotes: 2

Views: 795

Answers (2)

ComFreek
ComFreek

Reputation: 29444

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}$

Upvotes: 1

Sangeet Menon
Sangeet Menon

Reputation: 9915

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;
  }

}

Upvotes: 1

Related Questions