homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

Forcing a text input to only allow a set length of string in it.
Can I do it without using Javascript to check after they exit the field?

 3:59 pm on Aug 8, 2003 (gmt 0)

I would like to know if I can force an input text to a specific length. Example would be like this...

<input type="text" size="5">

that would give a text field that is 5 characters in length, but it will let you put more then 5 characters in it. I can just use Javascript and check the length and say it's not the right size, but I don't want them to even be able to put in 5 characters. Also, this could be extra, I would like to keep track of how many are already there and when it hits 5 jump to the next field on the screen.

I can always just use Javascript to do this, but I don't want to wait until they input and leave the field. I'm picky.

Thanks again everyone



 4:02 pm on Aug 8, 2003 (gmt 0)

well you can use javascript in the onchange event handler and check after each keypress, and delete the last letter if they enter too many.



 4:03 pm on Aug 8, 2003 (gmt 0)

You can limit textboxes with the maxlength attribute:

<input type="text" size="5" maxlength="5">



 4:03 pm on Aug 8, 2003 (gmt 0)

<input type="text" name="fieldName" maxlength="8">


 4:08 pm on Aug 8, 2003 (gmt 0)

Oh man again, asked to quickly.... maxlength does the first part of my question, anyone know about the second? Jumping to a new field right away after reaching the "maxlength" of the field?


 4:17 pm on Aug 8, 2003 (gmt 0)

You would need to use JavaScript for this:

function shiftFocus(fromField, fieldLength, toField)
if(fromField.length == fieldLength) {

Then in your button:

onkeyup= "return shiftFocus(document.form.fieldFrom.value, 8 ,document.form.FieldTo)"

Caveat: this is totally untested!



 4:28 pm on Aug 8, 2003 (gmt 0)

cool, I didn't know there was a onkeyup....I need to read more then 1 book for HTML hehe...

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved