Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    XML error messages in Javascript

    Hi everyone.

    I'm a little bit of a newbie here, so if you could please either help me answer this problem or point me somehwere where I might find the answer it would be greatly appreciated.

    I have an online form that has some (external) Javascript validation attached to it. I want to store the error messages in an external xml file and call them during the validation.

    Here is a sample of my xml file structure:

    Code:
    <Webform_Error_Messages>
    	
    	<blank_first_name>You must enter your first name.</blank_first_name>
    	
    </Webform_Error_Messages>
    Now, the reason I'm posting this here is that I believe my issue is in the Javascript integration. On form submit, the validate_webform function gets executed.

    Code:
    function loadXMLDoc(dname)
    {
    if (window.XMLHttpRequest)
      {
      xhttp=new XMLHttpRequest();
      }
    else
      {
      xhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xhttp.open("GET","dname",false);
    xhttp.send();
    return xhttp.responseXML;
    } 
    
    function validate_webform(thisform)
    {
    	xmlDoc=loadXMLDoc("webform_error_msgs_en.xml");
    	with (thisform)
    	{
    	 
    	 if (validate_required(first_name)==false)
    		{
    		 first_name_error.innerHTML = xmlDoc.getElementsByTagName("blank_first_name")[0].childNodes[0].nodeValue;
    		 return false;
    		 }
    }
    }
    What happens when I click submit is that the form goes ahead and submits, foregoing all validation. When I replace my xml error messages with a regular text string (i.e. first_name_error.innerHTML = "this is an error"), everything works great.

    I have also validated my xml document to ensure there are no errors in it.

    I realize that it may be a seemingly easy answer or a very obvious solution to some, but I've been searching for a solution going on 3 days now to no avail, so any help as to why this is not working would be greatly appreciated.

    Thank you for your time.

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    if it works with a plain string, I’d suspect a runtime error (e.g. some required object does not exist) which should be listed in the JS Error Console.


    besides that, I’d use JSON instead of XML (esp. since JSON is now built-in into most browsers) so your call would simplify to something like first_name_error.innerHTML = Error_Messages.blank_first_name;
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

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