/ Published in: JavaScript
Use this to validate if input has html elements
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
//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';