SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Oct 2005
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript Validation Problem

    I've wrote a validation script using PHP and Javascript and i keep getting a Javascript error box appear when i don't enter anything in the Confirm Email Box. I've looked in Firebug and it seems that the asyncrous call keeps looping without stopping or timing. Therefore i assume its looking for something thats not there. However i've checked my PHP scripts and i can't locate it. I've been told its in the Javascript file and in particualr line 135 and 172. Can anybody help?

    http://www.creativebubbles.co.uk/demo1/register/

    Code:
    // JavaScript Document
    
    var xmlHttp = createXmlHttpRequestObject();
    
    //hold the remote server address
    
    var serverAddress = "./../scripts/validate.php";
    
    //when set to true show detailed errors
    
    var showErrors = true;
    
    //initialize the validation requests cache
    
    var cache = new Array();
    
    
    function createXmlHttpRequestObject()
    {
    	var xmlHttp;
    
    	try 
    	{
    		xmlHttp = new XMLHttpRequest();
    	}
    	catch(e)
    	{
    	
    		var xmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
    										"MSXML2.XMLHTTP.5.0",
    										"MSXML2.XMLHTTP.4.0",
    										"MSXML2.XMLHTTP.3.0",
    										"MSXML2.XMLHTTP",
    										"Microsoft.XMLHTTP");
    		
    		
    		for (var i=0; i<xmlHttpVersions.length && !xmlHttp; i++)
    		{
    			try
    			{
    		
    			xmlHttp =  new ActiveXObject(xmlHttpVersions[i]);
    			}
    			catch(e) {}
    		}
    	}
    		
    		
    		
    		if(!xmlHttp)
    			alert("Error Creating the XMLHttpRequest object.");
    		else
    			return xmlHttp;
    }
    
    
    //function that displays the error message
    
    function displayError($message){
    	
    	//ignore errors if showErrors is false
    	
    	if (showErrors)
    	{
    		//turn error displaying off
    		showErrors = false;
    		//display error message
    		
    		alert("Error encountered: \n" + $message);
    		//retry validation after 10 seconds
    		setTimeout("validate();", 10000);
    	}
    	
    }
    
    //the function handles the validation for any form field
    
    function validate(inputValue, fieldID) {
    	
    	//only continue if xmlHttp isn't void
    	
    	if (xmlHttp)
    	{
    			//if we have recieved no nll parameters, we add them to cache in the
    			//form of the query string to be sent to the server for validation
    			if (fieldID)
    			{
    				//encode values for the safely adding them to an Http request query strong
    				
    				inputValue = encodeURIComponent(inputValue);
    				fieldID = encodeURIComponent(fieldID);
    				//add the values to the queue
    				cache.push("inputValue=" + inputValue + "&fieldID=" + fieldID);
    			}
    			//try and connecto to the server
    			try
    			{
    				//continue only if the XMLHttpRequest object isn't busy
    				//and the cache is not empty
    				
    				if ((xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
    					 && cache.length > 0)
    				{
    					//get a new set of parameters from the cache
    					var cacheEntry = cache.shift();
    					//makes a server request to validate the extracted data
    					xmlHttp.open("POST", serverAddress, true);
    					xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    					xmlHttp.onreadystatechange = handleRequestStateChange;
    					xmlHttp.send(cacheEntry);
    				}
    			}
    				catch (e)
    				{
    					//dipplay an error when failing to connect to the server
    					displayError(e.toString());
    				}
    	}
    	
    }
    
    
    	
    function handleRequestStateChange()
    	{
    	
    	
    		if (xmlHttp.readyState == 4)
    		{
    			
    			if (xmlHttp.status == 200)
    			{
    				try
    				{
    				  readResponse();
    				}
    				catch(e)
    				{
    				
    					displayError(e.toString());
    				}
    			}
    			else
    			{
    				
    				displayError(xmlHttp.statusText);
    			}
    		}
    	}
    	
    	
    function readResponse()
    	{
    	
    		var response = xmlHttp.responseText;
    		
    		//server error?
    		
    		if(response.indexOf("ERRNO") >= 0 || response.indexOf("errror:") >= 0 || response.length == 0)
    		throw(response.length == 0 ? "Server error." : response);
    		
    		//get the response in xml format
    		
    		responseXML = xmlHttp.responseXML;
    		//get document element
    		xmlDoc = responseXML.documentElement;
    		result = xmlDoc.getElementsByTagName("result")[0].firstChild.data;
    		fieldID = xmlDoc.getElementsByTagName("fieldid")[0].firstChild.data;
    		//find the HTML element that displays the error
    		message = document.getElementById(fieldID + "Failed");
    		//show the error or hide the error
    		message.className = (result == "0") ? "error" : "hidden";
    		//call validate () again, in case there are values left in the cahce
    		//setTimeout("validate();", 500);
    	
    			
    }
    
    
    function setFocus() {
    	
    	document.getElementById("txtName").focus();	
    	
    }

  2. #2
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    Location
    Belgium
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It seems I was wrong telling you to comment line 172, sorry about that.

    The script validates the input when a field lost focus.
    When you go and check something in Firebug and click something in Firebug, the input field loses focus, and validates the input.

    I've tested it more thoroughly now, with Firebug already open on the console tab, and there are only request made when a field loses focus.
    The script doesn't seem to validate any field by itself after not entering your e-mail address to confirm...
    FOR SALE: 1 set of morals, never used, will sell cheap

  3. #3
    SitePoint Evangelist
    Join Date
    Oct 2005
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'm a little confused. I know thats the reason why the scripts not working but i am still unsure how to resolve this or where excalty i can start to resolve this. Is it PHP or javascript related?

    Cheers

  4. #4
    SitePoint Evangelist
    Join Date
    Oct 2005
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    any ideas?

  5. #5
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    Location
    Belgium
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't see what the problem is. Your script works fine with me.
    If you click outside the viewport of the browser, it's normal that an input field loses focus, and thus trying to validate the user input for that input field.

    If you want to work around this, you'll have to find another way to start the validation process. For example, starting the validation process when a user stopped typing in an input field for a 1000 ms, instead of validating the user input after the field has lost focus.
    FOR SALE: 1 set of morals, never used, will sell cheap


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •