Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery validation to check text contains no html

How can I perform jquery validation to check if a textarea contains no html tags? (error if it does)

(BTW I am preventing html from coming through on the server anyway)

like image 543
raklos Avatar asked Mar 24 '11 16:03

raklos


People also ask

How do you check is jQuery empty or not?

In this article, we will check if an array is empty or not using jQuery. In JavaScript, arrays are a special type of object. If we use the typeof operator for arrays, it returns “object”. We can use jQuery's isEmptyObject() method to check whether the array is empty or contains elements.

How can check input value is number or not in jQuery?

version added: 1.7jQuery. The $. isNumeric() method checks whether its argument represents a numeric value. If so, it returns true . Otherwise it returns false .

How do you check HTML tags?

The valid HTML tag must satisfy the following conditions: It should start with an opening tag (<). It should be followed by a double quotes string or single quotes string. It should not allow one double quotes string, one single quotes string or a closing tag (>) without single or double quotes enclosed.


1 Answers

You could use John Resig's HTML parser (http://bit.ly/hsMq4x), or maybe a more naive solution that just looks for opening tags.

$('textarea').each(function() {
   if ($(this).val().match(/<(\w+)((?:\s+\w+(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)>/)) {
      alert('html found');
   }
});

Neal's solution suffers from false positives on textareas that contain valid jquery selectors, such as a textarea that contains just "a".

like image 162
xn. Avatar answered Nov 15 '22 14:11

xn.