469,326 Members | 1,549 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,326 developers. It's quick & easy.

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

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

Feb 1 '07 #1
2 2029
Lee
al****@gmail.com 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.
--

Feb 1 '07 #2
Daz
On Feb 1, 6:53 pm, alx...@gmail.com wrote:
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.

Feb 1 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Pekka Niiranen | last post: by
6 posts views Thread by Foxy Kav | last post: by
2 posts views Thread by Dr. Who | last post: by
6 posts views Thread by Du Dang | last post: by
4 posts views Thread by Earl | last post: by
9 posts views Thread by cheng | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.