1

I have a textarea field and I want the user to be not able to enter HTML TAGS it to textarea.

Is that possible?

EXAMPLE

    <textarea rows="4" cols="30" id="textareadescription"></textarea>

In this user not able to enter HTML TAGS like

      <button> Submit </button>
3
  • why you want to do that? you can trap keypresses using jquery/javascript and disable it. but that will be so annoying. instead let them write and upon submission, you filter out all the html tags by a simple string replace alogrithm Commented Nov 21, 2012 at 10:21
  • here. this is how you can capture a keypress Commented Nov 21, 2012 at 10:22
  • Disable the controle could help you? <textarea rows="4" cols="30" id="textareadescription" disabled="disabled"> Commented Nov 21, 2012 at 10:26

7 Answers 7

6

you can do it like this

function Check(){

var reg =/<(.|\n)*?>/g; 

if (reg.test($('#YourTextAreaID').val()) == true) {

    var ErrorText ='do not allow HTMLTAGS';

    alert('ErrorText');

    }
}

MORE INFO

http://www.pagecolumn.com/tool/all_about_html_tags.htm

Sign up to request clarification or add additional context in comments.

Comments

2

The way I would do it is as follows. This can be made more efficient by refactoring the replacements into a function. But I elaborated it to explain the procedure simply ...

           var str =   $('#textareadescription').val();
           //Temporarily replace the HTML elements I can accept
            str = str.replace(/<\/b>/g, "@@/b@@");
            str = str.replace(/<br>/g, "@@br@@");
            str = str.replace(/<i>/g, "@@i@@");
            str = str.replace(/<\/i>/g, "@@/i@@");

            //render all other HTML elements harmless
            str = str.replace(/</g, "&lt;");
            str = str.replace(/>/g, "&gt;");

            //put back the HTML elements I can accept
            str = str.replace(/@@b@@/g,"<b>");
            str = str.replace(/@@\/b@@/g,"</b>");
            str = str.replace(/@@i@@/g,"<i>");
            str = str.replace(/@@\/i@@/g,"</i>");
            str = str.replace(/@@br@@/g,"<br>");


//At this point str should contain only the HTML elements that I can accept. 

Comments

1

You can prevent the users from entering < and > signs, if those are not really required for your input text.

The following javascript code does so:

$(function(){
    $('#textareadescription').on("keydown", function(e){
        if (e.shiftKey && (e.which == 188 || e.which == 190)) {
            e.preventDefault();
        }
    });
});​

Try out the working demo here: http://jsfiddle.net/AHrrP/1/

3 Comments

wat if i want to write a text like this: 500>400 ? besides, i can always copy and paste
Valid point @Manish. Depends on how much particular Ishan wants to be.
What if user copy and paste test including HTML tags?
0

Below are the options you can go for:

  1. you can disable < or > symbol on your Textarea. this will show you how to do something on a particular key press.
  2. you let them write html tags in the textArea. upon submission, you reprocess the string and then send it to the server.

below is the script to reprocess your textArea content

function reProcessText(){

        var text= $('myTextArea').html();
        var startTag=-1;
        var endTag=-1;
        for(var i=0;i<text.length;i++){
            if(text[i]=='<')
             startTag=i;
        if(text[i]=='>')
             endTag=i;
        if(startTag != -1 && endTag!=-1){
             text = text.substring(0,startTag) + text.substring(startTag+1, endTag);

            startTag=endTag=-1;
          }
        }  
}

I have written a super simple algo to look for and replace html tags. for sure, you may have to employ more complex one, based on your need.

Comments

0

The scenario:

  1. User enters " Submit " in the textarea.
  2. Later on, you copy that value and show it in a div.

So what you want to do is:

  1. Make sure div is not showing a button. It will show the exact value what the user typed.

You can use jquery's html() and text() functions here.

For example: If you use this, the result div will generate a button.

    <textarea rows="4" cols="30" onkeyup="$('#divDesc').html($('#textareadescription').val());" id="textareadescription"></textarea>
    <div id="divDesc"> Result </div>

But if you use this, the result div will not generate button, but show the value:

    <textarea rows="4" cols="30" onkeyup="$('#divDesc').text($('#textareadescription').val());" id="textareadescription"></textarea>
    <div id="divDesc"> Result </div>

Comments

0

To strip out html tags, I use. mysqli_real_escape_string(); addslashes();

Comments

-2
<script>
    $(document).ready(function() {
        $('#description').summernote(
            {
                placeholder: 'Description',
                tabsize: 2,
                height: 100,
                toolbar: [
                    ['style', ['bold', 'italic', 'underline', 'clear']],
                    ['fontsize', ['fontsize']],
                    ['para', ['ul', 'ol', 'paragraph']],
                    ['height', ['height']]
                ],
                codemirror:{theme: 'monokai'},
                fontNames: ['Arial', 'Arial Black', 'Comic Sans MS', 'Courier New']
            }
        );
    });
</script>

1 Comment

<script>dhgsjdksidhsgjnnbhgsjbhjsjhghgshshshshhshshshhshs</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.