Question about <input type="text" name="firstname">, and auto-clearing contents

A

alxasa

Hello, I am hoping someone can help me with this. I need a javascript
function, which sits inside a <input type="text" name="firstname">
line of code. Now, if someone starts typing fine, but when it goes 1
character past 15 characters (15 characters only allowed), in this
case I would like the contents of the input to be cleared out
(automatically), and reset the input and its value back to nothing. a)
Can this be done, and will someone please show me to do?? :) Thank
you so much!! AngieX
 
L

Lee

(e-mail address removed) said:
Hello, I am hoping someone can help me with this. I need a javascript
function, which sits inside a <input type="text" name="firstname">
line of code. Now, if someone starts typing fine, but when it goes 1
character past 15 characters (15 characters only allowed), in this
case I would like the contents of the input to be cleared out
(automatically), and reset the input and its value back to nothing. a)
Can this be done, and will someone please show me to do?? :) Thank
you so much!! AngieX

Yes, it can be done, but it's a bad idea.

If you had typed 15 characters, then accidentally hit another
character instead of the TAB key, how would you feel if your
input was deleted? I'd be unhappy. If I was about to buy
something, I would find another vendor. If it was an internal
application, I would complain to the appropriate management.

Give them a chance to hit the backspace key to correct their
mistake. If they actually leave the field after entering too
many characters, alert them in some way and select the text they
typed so that they can easily choose to either delete it all and
start over or simply fix a typing mistake.


--
 
D

Daz

Hello, I am hoping someone can help me with this. I need a javascript
function, which sits inside a <input type="text" name="firstname">
line of code. Now, if someone starts typing fine, but when it goes 1
character past 15 characters (15 characters only allowed), in this
case I would like the contents of the input to be cleared out
(automatically), and reset the input and its value back to nothing. a)
Can this be done, and will someone please show me to do?? :) Thank
you so much!! AngieX


It would probably be wiser to just use the MAXLENGTH attribute for the
text input field. If you limit this to 15, and make it long enough to
fit only fifteen characters into, the user should soon notice they
can't fit more characters into it. Even better still would be to tell
the user that they shouldn't enter more than 15 characters. That way,
there can be no mistaking.

You will also need to take into account what would happen if the user
doesn't have JavaScript enabled, or if their browser doesn't support
it. All data should be validated on the server side, too. You should
not be relying on client side validation, as there is so much that can
go wrong, either with the browser, or due to the user changing the
source at their end. In general, JavaScript should only be used to
validate a user's input in such a way that it's not relied upon by the
code on your server. I often find it best to created my page, and
imagine that JavaScript does not exist. Once done, then I add the
JavaScript, so it will work with or without almost flawlessly.

Hope this helps.

Daz.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top