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)
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.
version added: 1.7jQuery. The $. isNumeric() method checks whether its argument represents a numeric value. If so, it returns true . Otherwise it returns false .
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.
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With