0
<input type="text" id="NotesReturn" />
<script type="text/javascript">
    $("#NotesReturn").keypress(function (e) {
         kCode = e.keyCode || e.charCode
         var htmlRegExp=new RegExp("/<(\w+)((?:\s+\w+(?:\s*=\s*(??:'[^']*')|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)>/");
         if (htmlRegExp.test(kCode) || $(this).val().length >= 512)
            e.preventDefault();
    });

    $("#NotesReturn").bind('paste', function(e){
         var pastedValue=this.value+window.clipboardData.getData('Text');
         var trimmedValue=pastedValue.substring(0, 512);
         var htmlRegExp = new RegExp("/<(\w+)((?:\s+\w+(?:\s*=\s*(??:'[^']*')|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)>/");
         if(!htmlRegExp.test(trimmedValue))
         {
             $(this).val(null);
             $(this).val(trimmedValue.toString());
         }
         e.preventDefault();
    });

</script>

My textbox should not allow html data.But it is throwing unexpected quantifier error.can someone help me.I could not figure out the error.

2
  • The parentheses in your regexp aren't balanced. And you shouldn't put / at the beginning and end when you use new RegExp(), you only do that when writing the regex as a literal. Commented Jan 8, 2013 at 3:18
  • I don't get "unexpected quantifier". Chrome complains "Invalid group". Commented Jan 8, 2013 at 3:19

1 Answer 1

1

The unexpected quantifier error is due to the ?? in (??: which should probably be simply (?: to indicate a non-capturing group.
As Barmer stated, the opening and closing forward slashes should not be included and the round brackets are unbalanced. You need to remove a right round bracket or add another left one.
You also need to escape the backslashes by preceding them with a backslash when using the RegExp constructor.
Doing the above will give you a valid regex but whether it will work correctly to disallow html is another question.

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

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.