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.