如何在jQuery中验证XML字符串? [英] How to validate xml string in jquery?

查看:92
本文介绍了如何在jQuery中验证XML字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个需要通过jquery编辑xml标记的项目中. 在以字符串形式保存xml之前,我想验证xml标签是否正确匹配,但在客户端使用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?

谢谢

推荐答案

我一直在尝试使用出色的jQuery

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');

这篇关于如何在jQuery中验证XML字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆