Need Pure/jQuery Javascript Solution For Cleaning Word HTML From Text Area

后端 未结 5 1389
慢半拍i
慢半拍i 2021-01-01 08:24

I know this issue has been touched on here but I have not found a viable solution for my situation yet, so I\'d like to but the brain trust back to work and see what can be

相关标签:
5条回答
  • 2021-01-01 08:39

    It might be useful to use the blur event which would be triggered less often:

    $("textarea").blur(function() {
        // check input ($(this).val()) for validity here
    });
    
    0 讨论(0)
  • 2021-01-01 08:50

    I am looking at David Archer's answer and he pretty much answers it. I have used in the past a solution similar to his:

    $("textarea").change( function() {
        // convert any opening and closing braces to their HTML encoded equivalent.
        var strClean = $(this).val().replace(/</gi, '&lt;').replace(/>/gi, '&gt;');
    
        // Remove any double and single quotation marks.
        strClean = strClean.replace(/"/gi, '').replace(/'/gi, '');
    
        // put the data back in.
        $(this).val(strClean);
    });
    

    If you are looking for a way to completely REMOVE HTML tags

    $("textarea").change( function() {
        // Completely strips tags.  Taken from Prototype library.
        var strClean = $(this).val().replace(/<\/?[^>]+>/gi, '');
    
        // Remove any double and single quotation marks.
        strClean = strClean.replace(/"/gi, '').replace(/'/gi, '');
    
        // put the data back in.
        $(this).val(strClean);
    });
    
    0 讨论(0)
  • 2021-01-01 08:59

    What about something like this:

    function cleanHTML(pastedString) {
        var cleanString = "";
        var insideTag = false;
        for (var i = 0, var len = pastedString.length; i < len; i++) {
            if (pastedString.charAt(i) == "<") insideTag = true;
            if (pastedString.charAt(i) == ">") {
                if (pastedString.charAt(i+1) != "<") {
                    insideTag = false;
                    i++;
                }
            }
            if (!insideTag) cleanString += pastedString.charAt(i);
        }
        return cleanString;
    }
    

    Then just use the event listener to call this function and pass in the pasted string.

    0 讨论(0)
  • 2021-01-01 08:59

    Edited from the jquery docs..

    $("textarea").change( function() {
        // check input ($(this).val()) for validity here
    });
    

    Thats for detecting the changes. The clean would probably be a regex of sorts

    edited above to look for a textarea not a textbox

    0 讨论(0)
  • 2021-01-01 09:01

    You could check out Word HTML Cleaner by Connor McKay. It is a pretty strong cleaner, in that it removes a lot of stuff that you might want to keep, but if that's not a problem it looks pretty decent.

    0 讨论(0)
提交回复
热议问题