Dynamic event handler onfocus() onblur() Javascript profesional needed

Posted on 2006-07-13
Last Modified: 2008-01-09
I have created a custom validation function, actually I have something like this :

<asp:textbox id='yo' runat='server' />
<script language='javascript'>
function checkCtrlyo(){

this works wondefull the function executes and shows(or not) a custom popup. Please keep in mind that the javascript code is generated automatically, then if I have a second textbox something like this will be generated:

<asp:textbox id='yoTheSecond' runat='server' />
<script language='javascript'>
function checkCtrlyoTheSecond(){

Validate is the generic function who does the validation. I have simplified everything a little, the validate function receives plenty of parameters. Now I need to catch the postback from the button on the page and validate the textboxes, now as the code is generated I do something like this:


function HandleButtons(){
    for(var i=0; i<document.forms.length; i++){
          for(var j=0;j<f.length;j++){
                   theglobal = false;
                   c.onclick=function(){return Cold_Validateall();};

function Cold_Validateall(){
         var tempy=true;
         for(var i=0;i<document.forms.length;i++){
               for(var j=0;j<f.length;j++){
                      if(c.type=='text' || c.type=='textarea'){
                      if (theglobal==false){tempy=false;}}}}
 return tempy;}

If you analyze those functions, I dynamically attach the function Validateall to all the buttons in the web page. The function Validateall simply do a loop on the controls of the page and tries to do a focus and blur of all the textboxes to activate the Validate of the control, then inside the validate I have theglobal var who it's putted to false if the validation was wrong.

now this should work, but in fact it doesn't :( if I have 3 boxes, only the last one is taken into consideration, if the first validates false and the 2 others true, then the postback happens, if the last textbox is false, then the postback is stopped...

Also under firefox, the onfocus and onblur provokes some strange error, I have readed that is a bug of firefox when using onblur and onfocus. but this is not the matter.

Now in short and in resume, I need a dynamic way to activate the validate function of each textbox and if any of them returns false, then stop the postback.

Thank you for the help.
Question by:Drakecoldwinter
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4

Author Comment

ID: 17111503
the code seems to work under firefox, but not under Ie :(

I need a way to cancel the postback if one of the controls is not okay

Author Comment

ID: 17111968
I can feel the bug...
I have a public var called global, then on the buttons on the blur I call ValidateControl and if the control is not okay, then I set global to false, then I do this :

loop each control{
 focus, then blur, then look the global var

then thing is, it looks like Ie is able to do focus, then blur and then test the global var before the validate function sets it to false... :( even more, I have several controls, so it must be able to loop all the controls (and thus have always global=true) before any of then has the chance of setting the var to false :( there is a detail that maked me think this, I putted an alert after the blur to check global, and with the alert everything works... :(

help ?

Author Comment

ID: 17140662

Ignore everything abobe, read this

Oki, lets try this otherwise, the full points go to the one who tells me how to execute and get the value of the function. validatecontrol returns true or false, how can I provoke and execution and get the true or false result. keep in mind this, if I do toto.onblur(); I will execute but I don't get the result (true or false) so I really need a way to get the result.

<input type='text' name="toto" onblur="validatecontrol('str','*@*.*','failed',100,20)">


Author Comment

ID: 17154602
well, I have found a solution by myself, is kind of not very elegant, but it works. I have managed to do a javascript array, and I fill on the server side page generation the array with the parameters that normally I use on validatecontrol function, then on my validateall function I simply take this array and cycle on it passing the parameters to Validatecontrol and like that I validate all the controls on the page. It works well.

I will close the question here then.

Accepted Solution

Netminder earned 0 total points
ID: 17155391
Closed, 250 points refunded.
Site Admin

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Tools for analysis of code 4 44
Compute age Html 2 24
Glitching Slide Show 19 26
Table header must be on top 2 25
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question