Tip: Validate text to prevent bugs

Wednesday, December 12, 2007 at 5:28 PM



As a gadget developer, it's your responsibility to make sure that the user can input only correct values in an edit box. For example, if an edit box can accept only numbers, handle the onkeypress event so that the user can enter only numbers.
function edit1_onkeypress() {
//48 and 57 represent the keycodes for '0' and '9'.
if ((event.keyCode < 48) || (event.keyCode > 57)) {
//The keycode is out of the allowed range. Cancel the event.
event.returnValue = false;
}
}
Have a tip you'd like to share? Send it to gd-developer AT google DOT com.

No comments: