homepage Welcome to WebmasterWorld Guest from 54.227.141.230
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor
Visit PubCon.com
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?
mikejson




msg:1472927
 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

 

killroy




msg:1472928
 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.

SN

mattur




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

You can limit textboxes with the maxlength attribute:

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

HTH

BlobFisk




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

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

mikejson




msg:1472931
 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?

BlobFisk




msg:1472932
 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) {
obj.select();
}
}

Then in your button:

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

Caveat: this is totally untested!

HTH

mikejson




msg:1472933
 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.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved