Go Premium for a chance to win a PS4. Enter to Win


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

Posted on 2006-07-13
Medium Priority
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
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Suggested Courses

926 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