0

I have applied a textbox click validation and wanted to avoid any html tags in text box also the simple < (open tag) and >(close tag). The below code is working for but i want to add additional validations also for empty strings and other tags in html. Can some one please help modify the regex for the requirement.

function htmlValidation() 
 {
   var re = /(<([^>]+)>)/gi;

   if (document.getElementById(’<%=TextBox2.ClientID%>’).value.match(re)){ document.getElementById(’<%=TextBox2.ClientID%>’).value = “”;
          return false;
        }
   return true;
 }

Corrected Code above

5
  • I don't exactly understand what your requirements are. Do you wan't to allow specific tags, or what behaviour do you exactly want? Commented Jul 17, 2012 at 10:13
  • I want to exclude any html tags in the textbox...also the "<" and ">" should not be allowed Commented Jul 17, 2012 at 10:19
  • Why not [^<>]+ ,and check if it does NOT match this pattern. Excluding the <> will also exclude any HTML tags. Commented Jul 17, 2012 at 10:19
  • @RoadBump: Works only if we anchor your Regexp at the beginning and end of the test string. Commented Jul 17, 2012 at 10:25
  • Although I don't know what exactly you are coding, but in many cases it would be better to let the user correct their input instead of you deleting it. < and > can also be escaped so that they are viewed as literals, not as html operators Commented Jul 17, 2012 at 10:28

2 Answers 2

3

In my opinion, I believe you'll have a good hard work if you want to validate such things.

Instead of preventing HTML content in a text box, other solution could be just html entity encode Text property, so <p>a</p> would be converted to &gt;p&lt;a&gt;p&lt;.

Result of that is you're going to render the HTML "as text" instead of getting it interpreted by Web browser.

Check this MSDN article:

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

6 Comments

I validating the user inputs on client side and there is requirement considering the html tags not allowed as it will hangup the other processes. So i im looking to solve at one click event only. If i get some answers will definelty post here
@PratikGupta But if you do this way, your processes will work as expected, because you won't have HTML!
there are many places where i specially don't want any special operators as this will give different meaning to the calculations handled hence i want to filter those things out on front end rather than at server side.
@PratikGupta I believe that an HTML entity isn't an special character. Elaborate more your case if you want further details.
Thank you so much for your help.I think i found the answer in my own question.I have made some mistake in the annotations of the javascript code. But now its validating all the HTML tags also inclosed in <any_tags_here>. I have also check your profile on stack it's really impressive.
|
0
$("#<%= btnAdd.ClientID %>").click(function () {
            var txt = $("#<%= txtBox1.ClientID %>");
            var svc = $(txt).val();  //Its Let you know the textbox's value 
            var re = /(<([^>]+)>)/gi;
            if(txt.val()!=""){
            if (!txt.val().match(re)) {
               //my Operations 
               //goes here
                });
                return false;
            }
            else {
                alert("Invalid Content");
            }
            }
            else {
            alert("Blank value selected");
            }

I have used Jquery function to check for regular expresion. This question is a linked question with Using Jquery to add items in Listbox from Textbox

Now i can mark this as my final answer.

Comments

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.