473,606 Members | 2,118 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

javascript onchange event handler

2 New Member
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 4128
gits
5,390 Recognized Expert Moderator Expert
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
lowslyn
2 New Member
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 Recognized Expert Moderator Expert
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
5183
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 complete in connection with attendance at a seminar. After spending more than 15 minutes on it, I clicked on the submit button - and nothing happened. Looking round the pages on Javascript form validation that Google produced for me (well,...
2
7036
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 and onChange handlers do some minor manipulation of form data (although they work on different form elements). If the onChange event fires, I need the form to be submitted. If the onClick event fires for a TR, I also need the form to be...
3
11465
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 'City' then you select 'Business Category' - now when you select 'Business Category' the next drop-down menu (business sub category) should populate with 'Business Sub-categories' related to the 'Business Category' but once 'Business Category' is...
13
31279
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 supposed to check all the checkboxes, which it does (that's not rocket science ;), but the 'onchange' event does not get triggered.
4
7731
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" onSubmit="return false;"> <input type="text" name="samplenum" value="" onChange="alert('onChange fired');"> <input type="submit" name="cmd_submit" value="Submit" onClick="return false">
7
3396
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 MyComboBox.Attributes.Add("OnChange", "return IndexChanged();"); but OnChange event never fires Thanks
2
2065
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 is not fired. Is there any way to make this work without having to change the code that changes Field A? Thanks, William
4
47966
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 onChange embedded in the "password" field. This is normal HTML and it works fine. <HTML> <HEAD><TITLE> Example of onChange Event Handler </TITLE> </HEAD> <BODY BGCOLOR="FFFFFF" TEXT="000000"> <H3>Example of onChange Event Handler</H3> <form...
19
2212
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? (in conjunction with css, you know, the usual...;) this is what is meant by "modern" javascript?? so how do folks feel about this who think javascript is so evil they disable it from their browsers?? do sites designed with "modern" javascript...
0
8009
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7939
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8428
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6753
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5962
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3919
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3964
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2442
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1548
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.