473,503 Members | 10,046 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

setting onBlur Programmatically

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 2738
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(document.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
9371
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...
3
8907
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...
1
2967
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...
18
18302
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...
2
1670
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...
2
1794
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: ...
1
5655
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...
0
2155
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...
4
6907
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...
0
7095
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...
0
7294
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,...
0
7361
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...
1
7015
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...
0
5602
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,...
1
5026
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...
0
3173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1523
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 ...
0
403
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...

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.