Finding text in a XML Node using Jquery Mobile

112 Views Asked by At

i am trying to parse an xml nd comparing its text with input fields but its not working here is the code i have tried..

 $('#finish').click(function() {
 var email = document.getElementById('email').value
    $.ajax({
        type: "GET",
        url: "test.xml",
        dataType: "xml",
        success: function(xml) {
            $(xml).find('child[name="Email"]').each(function(){
                var i = $(this).attr('text');
                if(i == email)
                alert('email already registered');
                else{
                activity.SubmitData();
                }

            });
        }
    });
});

Html

<input type="text" name="email" id="email" placeholder="Your Email..">
<button id="finish" >Register</button>

XML

<reg>
  <user>
    <Name> myName</Name>
    <Email> myEmail </Email>
    <Date> 22/12/2013 </Date>
  </user>
</reg>
2

There are 2 best solutions below

5
On BEST ANSWER

Try

$('#finish').click(function () {
    var email = document.getElementById('email').value
    $.ajax({
        type: "GET",
        url: "test.xml",
        dataType: "xml",
        success: function (xml) {
            //check if any Email element has the entered email as its value
            var valid = $(xml).find('Email').filter(function () {
                return $.trim($(this).text()) == email
            }).length == 0;
            //if valid submit the data
            if (valid) {
                activity.SubmitData();
            } else {
                alert('email already registered');
            }
        }
    });
});
3
On

You need to use == for comparison:

if(i == email)

Also, your input does not have text attribute so var i = $(this).attr('text'); will not work as expected. You're probably want to get its type instead:

var i = $(this).attr('type');

or if you want to get its text then use .text():

var i = $(this).text();

if you want to remove the trailing spaces from the text, then you can use $.trim():

var i = $.trim($(this).text());