473,437 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,437 software developers and data experts.

javascript onchange event handler

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
3 4103
gits
5,390 Expert Mod 4TB
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
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
5,390 Expert Mod 4TB
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

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

Similar topics

72
by: Stephen Poley | last post by:
I have quite often (as have probably many of you) come across HTML forms with irritating bits of Javascript attached. The last straw on this particular camel's back was a large form I was asked to...
2
by: Andy Goldstein | last post by:
I have a table where all the TRs have an onClick handler registered. One (and only one) of the rows has 2 text input boxes, where each textbox has an onChange handler registered. Both the onClick...
3
by: news.onetel.net.uk | last post by:
I and my friend Karl have spent literally all day trying to find out what is causing my error but we are zapped of any further functionality :) I have a form that adds news records. You select...
13
by: aundro | last post by:
Hello, I've been looking on the web for a solution to this problem: I create a set of checkboxes, and 2 buttons: - one is labeled "All" - the other is labeled "None" Clicking "All" is...
4
by: David McNerney | last post by:
Would anyone be able to tell me why I get an error in FireFox 1.5.0.1 for MacOSX when I type some text and hit Enter in the following form: <html> <body> <form action="http://example.com"...
7
by: Peter | last post by:
Does anyone know how to trap SelectedIndexChanged event on the client side for the following control? http://www.metabuilders.com/Tools/ComboBox.aspx I have tried...
2
by: William Cole | last post by:
Here is my issue I have two text fields (Field A, Field B). I want Field B to be updated when Field A is changed. The problem is Field A is being changed through JavaScript so an onChange event...
4
by: auslandt | last post by:
I have an HTML file that includes a JavaScript. I would like this JavaScript to be able to create an "onChange=<function>" attribute against the password element. Here is an example page of the...
19
by: maya | last post by:
hi, so what is "modern" javascript?? the same as "DOM-scripting"? i.e., editing content (or changing appearance of content) dynamically by massaging javascript objects, html elements, etc? ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.