Paras
Paras

Reputation: 3067

How to validate xml string in jquery?

I m working on a project which requires to edit the xml tags via jquery. Before I save the xml in the form of string, I wanted to validate if the xml tags matched properly but on the client side with jquery. Is there any simple way to achieve this?

Thanks

Upvotes: 2

Views: 5275

Answers (2)

Madbreaks
Madbreaks

Reputation: 19549

I was looking to validate a form text input as XML using the excellent jQuery Validation plugin, this ended up doing the trick:

$.validator.addMethod('XML', function(value, element){
    var isXml;
    if(!this.optional(element)){
        try{
            isXml = $.parseXML(value);
        }catch(e){
            isXml = false;
        }
    }
    return isXml !== false;
}, 'This is not valid XML');

Upvotes: 4

RoToRa
RoToRa

Reputation: 38420

jQuery has a $.parseXML() method since 1.5.

Upvotes: 3

Related Questions