I try to validate the Input in my website because of Cross-Site-Scripting-Attacks!
<form name="SearchInput" class="form-inline filterForm" method="post" action="/annoncen/" onsubmit="validateMyForm();">
<input type="text" name="searchWord" class="form-control form-search left" id="formGroupExampleInput" placeholder="Text" onkeyup="inputKeyUp(event)"/>
<input type="text" name="searchLoc" class="form-control form-search right" id="formGroupExampleInput" placeholder="Place" onkeyup="inputKeyUp(event)"/>
<button type="submit">search</button>
</form>
I use the validate() plugin to prevent the user put a script in the input field
function validateMyForm(){
var text_value = $('#formGroupExampleInput').val();
if(text_value!=='/^[a-zA-Z ]*$/') {
alert("Enter Some Text In Input Field");
event.preventDefault();
}}
but every time I get text_value ="" !!! What am I doing wrong
Note that anything in Javascript (validation, encoding or whatever else) won't protect against reflected or stored XSS. You need to encode your output where it's written back to the page (presumably on the server side), but that part is not shown in the question. XSS by default is an output encoding problem, and it is less about input valudation, though if strict enough and under the right circumstances, server-side input validation might also prevent it.
If however this is a DOM XSS you are trying to prevent, it would still be better to implement proper output handling in Javascript (like for example using
.text()of jQuery instead of.html()and so on) rather than trying to control input on the input field.