473,804 Members | 2,536 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

setting onBlur Programmaticall y

6 New Member
Hi all,
I have a web form that is generated on the fly. I need to insert some javascript into the form to do validation on the contents of each text box when the text box loses focus. here is what I have

Expand|Select|Wrap|Line Numbers
  1. <SCRIPT language=JavaScript>
  2. <!-- Begin
  3. function filterNum(obj) {
  4. re = /^$|!|$|<|>|"|'|%|,|&|%|;/g;
  5. // remove spcial charcters
  6. obj.value = obj.value.replace(re, "");
  7. }
  8. //  End -->
  9.  
  10. var oldonload = window.onload
  11. if (typeof window.onload != 'function') {
  12.    window.onload = mtc1006(document.ctl01);    
  13.   } else {
  14.     window.onload = function() {
  15.       if (oldonload) {
  16.         oldonload();
  17.       }
  18.       mtc1006(document.ctl01);
  19.     };
  20.   }
  21.  
  22. function mtc1006(obj)
  23. {
  24.      var children = obj.childNodes; 
  25.      for (var i=0; i<children.length; i++) {      
  26.               children[i].onBlur = filterNum(children[i]);
  27.               mtc1006(children[i]);                     
  28.       }                              
  29. }
  30. </SCRIPT>
  31.  
However, this doesn't work. Am I using the wrong syntax when setting the onBlur?
Apr 9 '08 #1
7 2763
gits
5,390 Recognized Expert Moderator Expert
hi ...

you need to 'closure' the incremented values:

Expand|Select|Wrap|Line Numbers
  1. for (var i = 0; i < children.length; i++) {      
  2.     children[i].onBlur = function(node) {
  3.         return filterNum(node);
  4.     }(children[i]);
  5.  
  6.     mtc1006(children[i]);                     
  7. }
  8.  
kind regards
Apr 9 '08 #2
ThePope78705
6 New Member
sorry to be a pest, here is the code i have now:
Expand|Select|Wrap|Line Numbers
  1. <SCRIPT language=JavaScript>
  2. <!-- Begin
  3. function filterNum(obj) {
  4. re = /^$|!|$|<|>|"|'|%|,|&|%|;/g;
  5. // remove spcial charcters
  6. obj.value = obj.value.replace(re, "");
  7. }
  8. //  End -->
  9.  
  10. var oldonload = window.onload
  11. if (typeof window.onload != 'function') {
  12.    window.onload = mtc1006(document.ctl01);    
  13.   } else {
  14.     window.onload = function() {
  15.       if (oldonload) {
  16.         oldonload();
  17.       }
  18.       mtc1006(document.ctl01);
  19.     };
  20.   }
  21.  
  22. function mtc1006(obj)
  23. {
  24.      var children = obj.childNodes; 
  25.      for (var i=0; i<children.length; i++) {      
  26.               children[i].onBlur = function(node) {
  27.                      return filterNum(node);
  28.                }(children[i]);
  29.  
  30.                mtc1006(children[i]);                   
  31.       }                              
  32. }
  33. </SCRIPT>
this still does not work. Do I need to change the way filterNum works? should that just take the string value to validate, and handle the setting of the textbox.value in the onBlur function? I'm not sure how I would do that. Any help would be greatly appreciated.
Apr 9 '08 #3
gits
5,390 Recognized Expert Moderator Expert
do you get any errors?
Apr 9 '08 #4
ThePope78705
6 New Member
When I run it in Firefox it does not generate any errors. However, in IE I get a "not implemented" error for the line :
window.onload = mtc1006(documen t.forms['ctl01']);

here is the full form I am testing, i added an additional condition to the loop to only adjust the onBlur property for "Input" tags:

Expand|Select|Wrap|Line Numbers
  1. <HTML><BODY>
  2.  
  3. <form name="ctl01" method="post" id="ctl01">
  4. <table class="text" border="0" cellpadding="3" cellspacing="1" width="600">
  5. <tbody>
  6. <tr bgcolor="#eeeeee">
  7. <td colspan="2" width="760"><strong><font face="Arial, Helvetica, sans-serif"><span class="style1 star style5">*</span><span class="style5"> </span></font></strong><strong><font face="Arial, Helvetica, sans-serif"><span class="style5"><font size="-2">denotes required field</font></span></font></strong></td></tr>
  8.  
  9. <tr bgcolor="#dddddd">
  10. <td><strong><font face="Arial, Helvetica, sans-serif">* First Name</font></strong></td>
  11. <td nowrap="nowrap"><font face="Arial, Helvetica, sans-serif"><table cellspacing="0" cellpadding="0" border="0" style="border-style:None;border-collapse:collapse;">
  12.     <tr valign="top">
  13.         <td align="left"><input name="FirstName" type="text" id="FirstName" IsRequired="True" style="" /></td>
  14.     </tr>
  15. </table></font></td></tr>
  16. <tr bgcolor="#eeeeee">
  17. <td><strong><font face="Arial, Helvetica, sans-serif"><span class="style5">* Last Name</span></font></strong></td>
  18. <td><font face="Arial, Helvetica, sans-serif"><table cellspacing="0" cellpadding="0" border="0" style="border-style:None;border-collapse:collapse;">
  19.     <tr valign="top">
  20.  
  21.         <td align="left"><input name="LastName" type="text" id="LastName" IsRequired="True" style="" /></td>
  22.     </tr>
  23. </table></font></td></tr>
  24. <tr bgcolor="#dddddd">
  25. <td><strong><font face="Arial, Helvetica, sans-serif"><span class="style1 star style5">Email</span></font></strong></td>
  26. <td><font face="Arial, Helvetica, sans-serif"></font><br><table cellspacing="0" cellpadding="0" border="0" style="border-style:None;border-collapse:collapse;">
  27.     <tr valign="top">
  28.         <td align="left"><input name="EmailAddress" type="text" id="EmailAddress" IsRequired="False" style="" /></td>
  29.     </tr>
  30. </table> </td></tr>
  31.  
  32. <tr bgcolor="#eeeeee">
  33. <td><strong><font face="Arial, Helvetica, sans-serif"><span class="style1 star style5">Company</span></font></strong></td>
  34. <td><font face="Arial, Helvetica, sans-serif"><table cellspacing="0" cellpadding="0" border="0" style="border-style:None;border-collapse:collapse;">
  35.     <tr valign="top">
  36.         <td align="left"><input name="Company" type="text" id="Company" IsRequired="False" style="" /></td>
  37.     </tr>
  38. </table></font></td></tr>
  39. <tr bgcolor="#eeeeee">
  40. <td><strong><font face="Arial, Helvetica, sans-serif"><span class="style1 star style5">Phone</span></font></strong></td>
  41. <td><table cellspacing="0" cellpadding="0" border="0" style="border-style:None;border-collapse:collapse;">
  42.     <tr valign="top">
  43.         <td align="left"><input name="WorkPhone" type="text" id="WorkPhone" IsRequired="False" style="" /></td>
  44.     </tr>
  45. </table> <br></td></tr>
  46. <tr bgcolor="#eeeeee">
  47. <td><strong><font face="Arial, Helvetica, sans-serif"><span class="style1 star style5">Integer</span></font></strong></td>
  48.  
  49. <td><table cellspacing="0" cellpadding="0" border="0" style="border-style:None;border-collapse:collapse;">
  50.     <tr valign="middle">
  51.         <td align="right"><span style="">inttest:&nbsp;</span></td><td align="left"><input name="inttest__c" type="text" id="inttest__c" IsRequired="False" /></td>
  52.     </tr>
  53. </table><br></td></tr>
  54. <tr>
  55. <td colspan="2">&nbsp; <input type="submit" name="mtcSubmit" value="Submit" id="mtcSubmit" style="" />&nbsp;  </td></tr>
  56. <tr>
  57. <td colspan="2">&nbsp;</td></tr></tbody></table>
  58. </form>
  59. <script type="text/javascript">
  60.  
  61. var oldonload = window.onload
  62. if (typeof window.onload != 'function') {
  63.    window.onload = mtc1006(document.forms['ctl01']);    
  64.   } else {
  65.     window.onload = function() {
  66.       if (oldonload) {
  67.         oldonload();
  68.       }
  69.       mtc1006(document.forms['ctl01']);
  70.     };
  71.   }
  72.  
  73. function mtc1006(obj)
  74. {
  75.      var children = obj.childNodes; 
  76.      for (var i=0; i<children.length; i++) {      
  77.               if (children[i].tagName == 'INPUT') {                    
  78.                      children[i].onBlur = function(node) {
  79.                            return filterNum(node);
  80.                      }(children[i]);
  81.               }
  82.  
  83.                mtc1006(children[i]);                   
  84.       }                              
  85. }
  86.  
  87. function filterNum(obj) {
  88. re = /^$|!|$|<|>|"|'|%|,|&|%|;/g;
  89. // remove spcial charcters
  90. obj.value = obj.value.replace(re, "");
  91. }
  92. </script>
  93. </BODY></HTML>
Apr 9 '08 #5
gits
5,390 Recognized Expert Moderator Expert
aaargh ... write the onBlur as onblur please ... is the handler-function called then?
Apr 9 '08 #6
ThePope78705
6 New Member
unfortunately not, I also tried children[i].onblur = new function(node)

that also did not work. :(
Apr 9 '08 #7
gits
5,390 Recognized Expert Moderator Expert
damned ... its too late today :) ... the following lines will do the job:

Expand|Select|Wrap|Line Numbers
  1. children[i].onblur = function(node) {
  2.     return function() { filterNum(node); };
  3. }(children[i]);
  4.  
kind regards
Apr 9 '08 #8

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

Similar topics

2
9391
by: Andrew Thompson | last post by:
Is it possible to set the onBlur of a link or other focusable element programmatically? A search of the archive showed plenty of examples of setting the onBlur() for each link or form element when it was defined, but I am after something that can iterate all links in the page and add a common onBlur functionality to every one.
3
8931
by: Robert Oschler | last post by:
I have a textarea element that I have created an onblur() handler for. In the onblur() handler, I check to make sure that they have saved the contents of the edit box, before leaving it. If they have not saved the contents, when they exit the edit box, I put up an alert telling them to perform the save. My handler (note: the editBox variable was properly assigned a reference to the desired HTML textarea element earlier):
1
2993
by: ofirpicazo | last post by:
Hi, my problem is that IE doesn't seem to do what it's supposed to, so I hope u guys can give me a hand. The code below is supposed to make text editable whenever it's clicked, and it works, the problem is that IE doesn't set the onClick attribute back to what I tell it to, at least it doesn't do anything if you click on the text again, as opposed to what Firefox does, FF works!. I put an alert on the onBlur event to see if IE sets the...
18
18449
by: Dixie | last post by:
Can I set the Format property in a date/time field in code? Can I set the Input Mask in a date/time field in code? Can I set the Format of a Yes/No field to Checkbox in code? I am working on a remote update of tables and fields and can't find enough information on these things. Also, how do you index a field in code?
2
1684
by: Jeffrey | last post by:
Hello I have several textboxes in a datagrid footer. When the user inputs something first textbox, the values of the other textboxes are updated. Basically I want the "onBlur" functionality of javascript here, but using codebehind. Why I don't want to use javascript for this is because I have to connect to my database and all the users' browsers have to be configured to allow "access to data source across domain" ( which doesn't seem...
2
1806
by: James Cooke | last post by:
I want to set body attributes programmatically, from the module file: I can go into the .aspx file and say <body onload="myFunc()"> but I don't want to. Like you do with a textbox control: txtCustomer.Attributes("onblur") = "myFunc()" I would like to do the same with the <body> tag.
1
5680
by: neil S via DotNetMonster.com | last post by:
I have a custom control with a textbox and dropdown list. The dropdown list is hidden and acts as a data source for the textbox. When the user enters text in the textbox, an onKeyup event is fired and retrieves the first value from the dropdown list equal to that entered, and fills the text box. Essentially, the two together are acting as a type ahead/autocomplete thingy. All this works fine. Here's my problem: I wanted to expose the...
0
2184
by: RKT | last post by:
I have a DataGridView bound to an MS Access table. This is a single- user application. When the User is adding or editing a row, the User may click on a Control elsewhere. That Control has context which I use to programmatically set *other* r/o cell values for the currently selected grid row. So, after that click on another control, and stuffing the related cell values, I want to seamlessly allow the user to resume editing/adding that row...
4
6947
by: rubenhan | last post by:
Hey, guys. My basic idea is this. When I write "1" in the pre-existing text field and onblur, I want to add a new field with a set of attributes, which turned out to be the same as the pre existing text field. (I don't want to use clonenode because of the compatibility issue) So I want that new text field to behave like the old text field. However, the new text field does not create another text field when I write "1" into it and onblur...
0
10594
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
10343
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
10331
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
10087
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9166
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...
0
6861
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5529
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...
1
4306
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
2
3831
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.