Category Archives: Javascript

Javascript: Limiting string length in textbox

I am wetting my beak in the Javascript world and I learn something new every day. When I faced this problem of limiting the length of string that the user can enter in a textbox, Google failed to return a satisfying result. My co-worker told me about the amazing attribute maxLength that is available for textbox. Works like magic.

If you want to restrict the length to 50, then just add an attribute ‘maxLength’ like this:

<input id="myTextBox" type="text" name="myTB" maxLength=50>

If you want to do this in javascript using jquery, then you need to add this line in your js file:

$("#myTextBox").attr("maxLength", 50);

I wonder why Silvelright textbox does not have something similar. This is a very helpful attribute for developers, do you agree?