Topic: Stopping a form from posting with javascript (Page 1 of 1) |
|
---|---|
Nervous Wreck (II) Inmate From: Kansas, just this side of the rainbow |
posted 07-06-2006 17:16
Okay, I admit up front, I do not know javascript. However, my work has asked me to build an intranet application that has grown much bigger than originally concieved (gotta love feature creep). code: function textLimit(field, maxlen) { if (field.value.length > maxlen) { field.value = field.value.substring(0, maxlen); alert("You have exceeded 150 characters"); return false; } }
|
Maniac (V) Mad Scientist From: 100101010011 <-- right about here |
posted 07-06-2006 17:21
Yep, |
Nervous Wreck (II) Inmate From: Kansas, just this side of the rainbow |
posted 07-06-2006 17:24
Sorry I forgot to add that part that I have. code: onSubmit="return textLimit(this.form.body, 150);"
|
Nervous Wreck (II) Inmate From: Kansas, just this side of the rainbow |
posted 07-06-2006 17:32
Okay, it didn't work, but I am wondering if because it is truncating the field which causes the form to then be true so it gets submitted? This is the same function that checks as you write in the field (using onkeyup event), so do I need a complete separate function for submitting? |
Maniac (V) Mad Scientist From: 100101010011 <-- right about here |
posted 07-06-2006 18:35
Yeah if you're calling this function onkeyup then there will never be more than 150 characters in the form field, because this will always truncate the stuff in there before beings submitted. |
Nervous Wreck (II) Inmate From: Kansas, just this side of the rainbow |
posted 07-06-2006 20:01
The problem is that if someone pastes some text into the field, it does not trigger the onkeyup event. So someone could paste text longer than 150 characters into the field and hit submit and it would carry over without a warning to the user. |
Maniac (V) Mad Scientist From: 100101010011 <-- right about here |
posted 07-06-2006 20:10
Yeah well the code above should work. code: function textLimit(field, maxlen) { alert(field.value); if (field.value.length > maxlen) { field.value = field.value.substring(0, maxlen); alert("You have exceeded 150 characters"); return false; } else { return true; } }
|
Nervous Wreck (II) Inmate From: Kansas, just this side of the rainbow |
posted 07-06-2006 20:52
Okay I finally figured it out. quote:
code: onSubmit="return textLimit(this.body, 150)"
code: onkeyup="textLimit(this.form.body, 150);"
|
Paranoid (IV) Inmate From: Norway |
posted 07-06-2006 21:12
bare in mind that you can also PASTE something in a form field using the mouse, so you should replace the onkeyup by an onchange quote: Form fields have a form property. Thus from any form field it is possible to go "one level up" to reach their owner FORM tag then access any of its form fields. code: onkeyup="textLimit(this, 150);"
|
Nervous Wreck (II) Inmate From: Kansas, just this side of the rainbow |
posted 07-06-2006 22:24
Actually, onchange did not activate when I pasted into the textarea, so it did not do anything different than onkeyup. |
Paranoid (IV) Inmate From: Norway |
posted 07-06-2006 22:42 |
Paranoid (IV) Inmate From: USA |
posted 07-06-2006 23:55
also just note that people can (and may?) bypass this entirely by disabling Javascript, or by using a browser that doesn't have Javascript (e.g. lynx). So, if you're really concerned about those 150 characters, you should also have some sort of server-side check (which may itself simply be truncating to 150 characters). |