Return to Snippet

Revision: 43216
at March 19, 2011 07:30 by anagaiyahoocom


Initial Code
//Testing for html in input box value
//e.g. '<script>' will validate true and be unsafe for processing

alert(/^(?:[^<]*(<[\w\W]+>)[^>]*$|#([\w\-]+)$)/.test($j('#sometxt').val()) ? 'unsafe' : 'safe');


<input type="text" id="sometxt" \>

//For PHP. Will validate to true and unsafe

$val = '<script>do.bad();</script>';

$patt = "/^(?:[^<]*(<[\w\W]+>)[^>]*$|#([\w\-]+)$)/";

echo preg_match($patt,$val) ? 'unsafe' : 'safe';

Initial URL

                                

Initial Description
Use this to validate if input has html elements

Initial Title
Testing for HTML Text Using Regular Expression

Initial Tags
javascript

Initial Language
JavaScript