Welcome to WebmasterWorld Guest from 54.145.39.186

Forum Moderators: open

Message Too Old, No Replies

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

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

10+ Year Member



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

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

WebmasterWorld Senior Member 10+ Year Member



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

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

WebmasterWorld Senior Member 10+ Year Member



You can limit textboxes with the maxlength attribute:

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

HTH

BlobFisk

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

WebmasterWorld Senior Member blobfisk is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

mikejson

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

10+ Year Member



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

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

WebmasterWorld Senior Member blobfisk is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

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

10+ Year Member



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

Featured Threads

Hot Threads This Week

Hot Threads This Month