By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,833 Members | 2,076 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,833 IT Pros & Developers. It's quick & easy.

javascript onchange event handler

P: 2
hi,

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
  1. function validate(val, name){
  2.   if(trim(val) == '') {
  3.     val=0;
  4.     document.getElementById(name).value = val;
  5.   }
  6. }
  7.  
Expand|Select|Wrap|Line Numbers
  1. <input type="text" id="textfield" name="textfield" onChange="validate(this.value, this.id)">
  2.  
  3.  
Sep 27 '07 #1
Share this Question
Share on Google+
3 Replies


gits
Expert Mod 5K+
P: 5,234
hi ...

welcome to TSDN ...

please show your trim-function too ... i guess your condition fails ... have a look at the following:

Expand|Select|Wrap|Line Numbers
  1. '' == 0 ; // evaluates to true
  2. '    ' == 0; // evaluates to true
  3.  
  4. // but:
  5.  
  6. '' === 0;  // evaluates to false
kind regards
Sep 27 '07 #2

P: 2
thanks for replying to my post.

i don't think it's the trim function because when i do an alert() as the first line in the validate function, it doesn't show up the alert message. i tried the code without using the trim() and it's still the same result. it seems it doesn't invoke the onchange event on the second delete and i'm wondering why?

anyways, if you want to take a look at the trim function, here it is...

Expand|Select|Wrap|Line Numbers
  1. function trim(s) {
  2.   while (s.substring(0,1) == ' ') {
  3.     s = s.substring(1,s.length);
  4.   }
  5.   while (s.substring(s.length-1,s.length) == ' ') {
  6.     s = s.substring(0,s.length-1);
  7.   }
  8.   return s;
  9. }
  10.  

hi ...

welcome to TSDN ...

please show your trim-function too ... i guess your condition fails ... have a look at the following:

Expand|Select|Wrap|Line Numbers
  1. '' == 0 ; // evaluates to true
  2. '    ' == 0; // evaluates to true
  3.  
  4. // but:
  5.  
  6. '' === 0;  // evaluates to false
kind regards
Sep 27 '07 #3

gits
Expert Mod 5K+
P: 5,234
hmmm ...

FF is doing well while IE does not ... may be you could try the onblur-event ... but be aware: that wouldn't fire when focus is not leaving the field ...

kind regards
Sep 27 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.