473,698 Members | 1,996 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 4144
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
5200
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
7042
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
11469
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
31395
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
7739
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
3404
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
2073
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
47977
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
2226
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
8603
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
9157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9027
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...
1
8895
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7725
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
6518
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
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2329
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.