can someone please help me with javascript...
i have this code that checks a text field when the user overrides the value to blank/space. if it is blank/space, i need to change the value to zero. it messes up the some computation when the value is blank so i need to change this to zero.
what is did was to add an onchange event handler in the text field. this calls the validate function which does the requirement of changing to zero when changed to blank.
my problem is when the user does this:
ex: text field = 12345
1. user deletes the value of text field making it blank. when user tabs out from the text field or clicks somewhere else, text field = 0 (this part is still ok, it passed the requirement)
2. however, when user deletes the value of the text field making it blank again, when the user tabs out from the text field or click somewhere else, the text field remains blank. it does not change to 0. it seems that it does not invoke the function on my onchange event.
can anyone please explain why it did not change to 0 again? any suggestions on how can i make it work?
thanks in advance.
Expand|Select|Wrap|Line Numbers
- function validate(val, name){
- if(trim(val) == '') {
- val=0;
- document.getElementById(name).value = val;
- }
- }
Expand|Select|Wrap|Line Numbers
- <input type="text" id="textfield" name="textfield" onChange="validate(this.value, this.id)">