Solved

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

Posted on 2006-07-13
5
1,067 Views
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'>
document.getElementById('yo').onblur=checkCtrlyo;
function checkCtrlyo(){
Validate('yo');
}
</script>

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'>
document.getElementById('yo').onblur=checkCtrlyoTheSecond;
function checkCtrlyoTheSecond(){
Validate('yoTheSecond');
}
</script>

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:

window.onload=HandleButtons;

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


function Cold_Validateall(){
         var tempy=true;
         for(var i=0;i<document.forms.length;i++){
               f=document.forms[i];
               for(var j=0;j<f.length;j++){
                      c=f[j];
                      if(c.type=='text' || c.type=='textarea'){
                           try{c.focus();c.blur();}catch(e){}finally{}
                      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.
0
Comment
Question by:Drakecoldwinter
  • 4
5 Comments
 
LVL 2

Author Comment

by:Drakecoldwinter
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
0
 
LVL 2

Author Comment

by:Drakecoldwinter
ID: 17111968
humm
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 ?
0
 
LVL 2

Author Comment

by:Drakecoldwinter
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)">

thanks
0
 
LVL 2

Author Comment

by:Drakecoldwinter
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.
0
 
LVL 5

Accepted Solution

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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now