Hi,
I have a strange problem, my code is running too quickly. Allow me to explain (if I can).
I am using the XMLHTTP object in order to query the database as a user completes the membership application form, basically I want to ensure that each username is unique.
I have the code to check this and the XMLHTTP object is working fine. However, before the code finishes executing the next part of the function is run. this means that the array I have for storing if items are valid or not is being populated incorrectly.
I only check the database if the first stage of validation returns true. At present I have a bit of a fudge in place in that I have added an alert box, as you can see in the code below, this seems to pause the code, which then results in the correct behaviour. Without this I get told that the username I entered is in the database but the system allows me to continue because the validation flag says true because the user name followed the regular expression in place.
Here is the code
[CODE=javascript]
function writeResponse()
{
if (goXMLHTTP.read yState==4 || goXMLHTTP.ready State=="complet e")
{
// I did hav a line here testing status=200 but it didn't make any difference document.getEle mentById(gcItem ID).innerHTML=g oXMLHTTP.respon seText;
// set glIsValid based on the return from the database
glIsValid = goXMLHTTP.respo nseText.search(/warninglabel/i)>0? false:true;
}
}
function GetXmlHttpObjec t()
{
if (window.XMLHttp Request)
{
goXMLHTTP=new XMLHttpRequest( )
}
else if (window.ActiveX Object)
{
goXMLHTTP=new ActiveXObject(" Microsoft.XMLHT TP")
}
if (goXMLHTTP==nul l)
{
alert ("Browser does not support HTTP Request")
return
}
}
function validateItem(pc ID,pcItem,pcDis play,pnType,plP opulateExtra,pn NumberOfItems,p lCheckDatabase, pnListItem)
{
var lcRegExp, llUseRegExp
var lnItemsToValida te = 0
glIsValid = false
llUseRegExp = false
switch (pnType)
{
case 1: // UK postcode
pcItem = pcItem.toUpperC ase()
lcRegExp = '^[A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA$'
break;
case 2: // email address
lcRegExp = '^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*$'
break;
case 3: // number of set length - basic telephone number check
lcRegExp = '^[0-9 ]{6,13}$'
break;
case 4: // password validation - alpha numeric characters 6-18 characters long
lcRegExp = '^[A-Za-z0-9]{6,18}$'
break;
case 5: // username validation - alpha numeric characters 6-18 characters long
lcRegExp = '^[A-Za-z0-9]{6,18}$'
break;
default: // all other validation items
lcRegExp = '^[A-Za-z0-9 ]{2,30}$'
break;
}
glIsValid = pcItem.match(lc RegExp)?true:fa lse;
// set the display now
if (plCheckDatabas e) // check the database
{
if(glIsValid) // only check the database if the item to check is valid from the first stage
{
GetXmlHttpObjec t() // sets the global variable goXMLHTTP
gcItemID = pcID
gcUrl = "../lib/datacheck.php?c heck=" + pnType +"&tocheck=' " + pcItem + "'"
goXMLHTTP.onrea dystatechange=w riteResponse
goXMLHTTP.open( "GET",gcUrl,tru e)
goXMLHTTP.send( null)
// update gaValidationLis t accordingly
alert("Checking the database for availability" + '\n' + "Click 'OK' to continue") // this is used to simply prevent the code from running ahead of itself and explain the process to the user
// it would be good if a better way could be established - THIS IS THE FUDGE
setValidationLi st(pnListItem)
}
}
else // set the display
{
document.getEle mentById(pcID). innerHTML = glIsValid? pcDisplay:"<spa n class='warningl abel'>" + pcDisplay + "</span>";
// update gaValidationLis t accordingly
setValidationLi st(pnListItem)
}
// structure to ensure that the form is only valid when it is valid
for (lnItem in gaValidationLis t)
{
if (gaValidationLi st[lnItem] == false)
{
lnItemsToValida te++
}
}
if (lnItemsToValid ate == 0)// everything is valid - let the user continue by setting the disabled property of an item
{
hideOrShowInput ("complete",fal se)
}
else // something is invalid, the user will be able to tell, they need to complete this before moving on
{
hideOrShowInput ("complete",tru e)
}
if (plPopulateExtr a)
{
populateExtra(l cSource, lcDestination, true, true)
}
}
function setValidationLi st(pnListItem)
{
// gaValidationLis t is a global array - for the life of the page using it at least
gaValidationLis t[pnListItem] = glIsValid
}
[/CODE]
That's all the functions involved at this stage. The PHP file referenced there simply runs some data checks and writes the relevant response back to the screen, so if the item exists the label written to the screen will contain the string "warninglab el"
Is there a better way of pausing the code? I tried setTimeout but that didn't have the desired results.
Any suggestions at all will be welcome. Generally I am quite pleased with this form and the way it validates as the user types - the validateItem() function is called from the onchange event of the input boxes.
Many thanks
Nathan
I have a strange problem, my code is running too quickly. Allow me to explain (if I can).
I am using the XMLHTTP object in order to query the database as a user completes the membership application form, basically I want to ensure that each username is unique.
I have the code to check this and the XMLHTTP object is working fine. However, before the code finishes executing the next part of the function is run. this means that the array I have for storing if items are valid or not is being populated incorrectly.
I only check the database if the first stage of validation returns true. At present I have a bit of a fudge in place in that I have added an alert box, as you can see in the code below, this seems to pause the code, which then results in the correct behaviour. Without this I get told that the username I entered is in the database but the system allows me to continue because the validation flag says true because the user name followed the regular expression in place.
Here is the code
[CODE=javascript]
function writeResponse()
{
if (goXMLHTTP.read yState==4 || goXMLHTTP.ready State=="complet e")
{
// I did hav a line here testing status=200 but it didn't make any difference document.getEle mentById(gcItem ID).innerHTML=g oXMLHTTP.respon seText;
// set glIsValid based on the return from the database
glIsValid = goXMLHTTP.respo nseText.search(/warninglabel/i)>0? false:true;
}
}
function GetXmlHttpObjec t()
{
if (window.XMLHttp Request)
{
goXMLHTTP=new XMLHttpRequest( )
}
else if (window.ActiveX Object)
{
goXMLHTTP=new ActiveXObject(" Microsoft.XMLHT TP")
}
if (goXMLHTTP==nul l)
{
alert ("Browser does not support HTTP Request")
return
}
}
function validateItem(pc ID,pcItem,pcDis play,pnType,plP opulateExtra,pn NumberOfItems,p lCheckDatabase, pnListItem)
{
var lcRegExp, llUseRegExp
var lnItemsToValida te = 0
glIsValid = false
llUseRegExp = false
switch (pnType)
{
case 1: // UK postcode
pcItem = pcItem.toUpperC ase()
lcRegExp = '^[A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA$'
break;
case 2: // email address
lcRegExp = '^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*$'
break;
case 3: // number of set length - basic telephone number check
lcRegExp = '^[0-9 ]{6,13}$'
break;
case 4: // password validation - alpha numeric characters 6-18 characters long
lcRegExp = '^[A-Za-z0-9]{6,18}$'
break;
case 5: // username validation - alpha numeric characters 6-18 characters long
lcRegExp = '^[A-Za-z0-9]{6,18}$'
break;
default: // all other validation items
lcRegExp = '^[A-Za-z0-9 ]{2,30}$'
break;
}
glIsValid = pcItem.match(lc RegExp)?true:fa lse;
// set the display now
if (plCheckDatabas e) // check the database
{
if(glIsValid) // only check the database if the item to check is valid from the first stage
{
GetXmlHttpObjec t() // sets the global variable goXMLHTTP
gcItemID = pcID
gcUrl = "../lib/datacheck.php?c heck=" + pnType +"&tocheck=' " + pcItem + "'"
goXMLHTTP.onrea dystatechange=w riteResponse
goXMLHTTP.open( "GET",gcUrl,tru e)
goXMLHTTP.send( null)
// update gaValidationLis t accordingly
alert("Checking the database for availability" + '\n' + "Click 'OK' to continue") // this is used to simply prevent the code from running ahead of itself and explain the process to the user
// it would be good if a better way could be established - THIS IS THE FUDGE
setValidationLi st(pnListItem)
}
}
else // set the display
{
document.getEle mentById(pcID). innerHTML = glIsValid? pcDisplay:"<spa n class='warningl abel'>" + pcDisplay + "</span>";
// update gaValidationLis t accordingly
setValidationLi st(pnListItem)
}
// structure to ensure that the form is only valid when it is valid
for (lnItem in gaValidationLis t)
{
if (gaValidationLi st[lnItem] == false)
{
lnItemsToValida te++
}
}
if (lnItemsToValid ate == 0)// everything is valid - let the user continue by setting the disabled property of an item
{
hideOrShowInput ("complete",fal se)
}
else // something is invalid, the user will be able to tell, they need to complete this before moving on
{
hideOrShowInput ("complete",tru e)
}
if (plPopulateExtr a)
{
populateExtra(l cSource, lcDestination, true, true)
}
}
function setValidationLi st(pnListItem)
{
// gaValidationLis t is a global array - for the life of the page using it at least
gaValidationLis t[pnListItem] = glIsValid
}
[/CODE]
That's all the functions involved at this stage. The PHP file referenced there simply runs some data checks and writes the relevant response back to the screen, so if the item exists the label written to the screen will contain the string "warninglab el"
Is there a better way of pausing the code? I tried setTimeout but that didn't have the desired results.
Any suggestions at all will be welcome. Generally I am quite pleased with this form and the way it validates as the user types - the validateItem() function is called from the onchange event of the input boxes.
Many thanks
Nathan
Comment