Sunday, 19 October 2014

Check if input is unique against xml file using javascript



I have a html page that allows users to register with an email, it stores it in an XML file.


I want to know what might be going wrong with my script below. But it doesn't seem to be working, as my test alert isn't going off when it should. It should be grabbing all of the email elements into an array and then looping through the array checking against the userinput. If it finds a match it should return false and alert me.



var Email = document.getElementById("Email").value;
var result = true;

var request = new XMLHttpRequest();
request.open("GET", "user.xml", false);
request.send();
var xml = request.responseXML;
var xmlEmails = xml.getElementsByTagName("email");
for(var i = 0; i < xmlEmails.length; i++) {
var xmlEmail = xmlEmails[i];
for(var j = 0; j < email.length; j++) {
if (email == email[j].childNodes[j].nodeValue)
{ result = false;
alert("testing");

}
}


Here's the xml file data



<?xml version="1.0"?>
<customers>
<customer>
<id>0</id>
<firstname>John</firstname>
<lastname>Doe</lastname>
<email>JohnDoe@mia.com</email>
<password>insecure</password>
</customer>
<customer>
<id>1</id>
<firstname>Jane</firstname>
<lastname>Doe</lastname>
<email>JaneDoe@mia.com</email>
<password>forgetful</password>
</customer>
</customers>


Thanks :).


No comments:

Post a Comment