趁還算清醒將程式碼紀錄一下:xml linq 抽取、ckeditor 驗證、常用去html標簽用法

本筆記完全沒參考價值
看得懂就懂,看不懂我自已看得懂就好XD


開始嘍!
資料如下:
<div key='3'>
        <div class='block' key='block0'>
            <div class='title_diamond'>ASDFASDFAsdf</div>
            <ul>
                <li class='none'>asdf</li>
                <li>asfas</li>
                <li>dfsadf</li>
                <li>asd</li>
                <li class='dining_table'>sadfasasdf</li>
                <li class='none'>sadf</li>
                <li>asdf</li>
                <li>asdf</li>
                <li></li>
                <li class='dining_table'>sadfasdf</li>
                <li class='none'>sadfasfd</li>
                <li>asdf</li>
                <li>asd</li>
                
            </ul>
        </div>
        <div class='block' key='block1'>
            <div class='title_diamond'></div>
            <ul>
                <li class='none'></li>
                <li class='dining_table'></li>
                <li class='none'></li>
                <li class='dining_table'></li>
                <li class='none'></li>

            </ul>
        </div>
        <div class='block' key='block2'>
            <div class='title_diamond'></div>
            <ul>
                <li class='none'></li>
                <li class='dining_table'></li>
                <li class='none'>asdfasfd</li>
                <li class='dining_table'></li>
                <li class='none'></li>
            </ul>
        </div>
    </div>


1 、xml 抽取linq,外有包迴圈:
    //將xml檔案放入
    XElement xDoc = XElement.Parse(text);
    //這裡設定key做為抓取條件
    var messagesList = (from project in xDoc.Elements("div").Where(d => d.Attribute("key").Value == "block" + i)
                        select project);
    //抓出標題
    var Project_Title = (from dr in messagesList.Elements("div")
                            select dr).First().Value;
    //抓出內容
    var ullist = (from dr in messagesList.Elements("ul").Elements("li")
                    select dr.Value).ToList();

    //放入list列表,比較方便處理
    List<string> tt = new List<string<();
    foreach (var tempword in ullist)
    {
        tt.Add(tempword);
    }

2、ckeditor 驗證輸入,條件為不得超過10行,字數為330字內,且必須依固定格式輸入:
<script type="text/javascript">

        //其中的<%=ckeditor1.ClientID %&gt為從後端傳過來的id值,這樣才抓得到
    $(function () {
          //按下確認後,送出前檢查
        $('#ASPform').submit(function () {

            var text =  CKEDITOR.instances.<%=ckeditor1.ClientID %>.getData();      
            if(text.indexOf('<ul>')==-1 || text.indexOf('<li>')==-1){
                alert('由於版型配置,請勿刪除預設版型喔…');
                return false;
            }else{
                text = text.replace(/<[^>]*>/gi, '');
                text.replace('\r\n','');
                text.replace('\r','');
                text.replace('\n','');
                text.replace(' ','');

                if (text.length >340) {
                    alert('由於版型備置若輸入過多會造成版型錯亂,還請修訂文案。');
                    return false;
                }
                else {
                    return true;
                }
            }
        });

        CKEDITOR.on('instanceCreated', function(e) {
            e.editor.on('contentDom', function() {
                e.editor.document.on('keyup', function(event) {
                    var text = CKEDITOR.instances.<%=ckeditor1.ClientID %>.getData();
                    //驗證有無刪掉預設版型
                    if(text.indexOf('<ul>')==-1 || text.indexOf('<li>') ==-1){
                        alert('由於版型配置,請勿刪除預設版型喔…');
                        CKEDITOR.instances.<%=ckeditor1.ClientID %>.setData("<ul><li></li></ul>");
                        CKEDITOR.instances.<%=ckeditor1.ClientID %>.focus();
                    }
                    //驗證有無超過十行
                    if((text.match(/<\/li>/g) || []).length > 9){
                        alert('由於版型配置,請勿超過十行喔…');
                        text = CKEDITOR.instances.<%=ckeditor1.ClientID %>.getData();
                        var lastIndexliStart = text.lastIndexOf('<li>');
                        text = text.substring(0,lastIndexliStart);
                        text +='</ul>';
                        CKEDITOR.instances.<%=ckeditor1.ClientID %>.setData(text);
                        CKEDITOR.instances.<%=ckeditor1.ClientID %>.focus();
                    }
                                                
                    //驗證有無換行符號
                    if(text.indexOf('<br />') > -1 ){
                        alert('由於版型配置,請依預設格式輸入喔…');
                        text = CKEDITOR.instances.<%=ckeditor1.ClientID %>.getData();
                        text = text.replace(/<br \/>/g,'');
                        CKEDITOR.instances.<%=ckeditor1.ClientID %>.setData(text);
                        CKEDITOR.instances.<%=ckeditor1.ClientID %>.focus();
                    }   
                        
                    //驗證有無超過字數,先去掉html語法
                    text = text.replace(/<[^>]*>/gi, '');
                    text.replace('\r\n','');
                    text.replace('\r','');
                    text.replace('\n','');
                    text.replace(' ','');
                    if (text.length >340) {
                        alert('由於版型配置,您輸入過多文字,還請修訂文案。'); 
                    }
                });  
            });
        });

    });    
</script>



後台C#去html語法,可以清掉前台輸入進來的html標箋
using System.Text.RegularExpressions;

...
...
...

 string sub_Item_Content2 = Regex.Replace(text , "<.*?>", string.Empty);

留言

熱門文章