使用val()验证文本输入

时间:2018-07-12 12:35:33

标签: jquery xss validating-event

由于跨站点脚本攻击,我试图验证我网站上的输入!

<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>

我使用validate()插件来防止用户在输入字段中放置脚本

function validateMyForm(){
       var text_value = $('#formGroupExampleInput').val();
       if(text_value!=='/^[a-zA-Z ]*$/') {
            alert("Enter Some Text In Input Field");
            event.preventDefault();
       }}

但是每次我得到text_value =""时!我在做什么错

3 个答案:

答案 0 :(得分:0)

请注意,Javascript中的任何内容(验证,编码或其他任何内容)都无法防止反射或存储的XSS。您需要对输出的编码进行编码,然后将其写回到页面(大概在服务器端),但是问题中未显示该部分。默认情况下,XSS是一个输出编码问题,与输入评估无关紧要,尽管如果足够严格且在适当的情况下,服务器端输入验证也可能会阻止它。

但是,如果这是您要阻止的DOM XSS,则最好在Javascript中实现适当的输出处理(例如,使用jQuery的.text()而不是.html()等) ),而不是尝试控制输入字段上的输入。

答案 1 :(得分:0)

您的值始终为空,因为有两个输入具有相同的ID。 这是一个具有两个不同ID的示例(formGroupExampleInput和formGroupExampleInput1)

$( document ).ready(function() {
    $("button").on("click", function (e){    	
      var text_value = $('#formGroupExampleInput').val();
      console.log('your value ' + text_value);
       if(text_value==="") {
            alert("Enter Some Text In Input Field");
            e.preventDefault();
       }
    	 console.log('form is submited');
       //todo remove this e.preventDefault(); to make your form submit
       e.preventDefault();
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="SearchInput" class="form-inline filterForm" >
          <input type="text" name="searchWord" class="form-control form-search left" id="formGroupExampleInput" placeholder="Text" />
          <input type="text" name="searchLoc"  class="form-control form-search right" id="formGroupExampleInput1" placeholder="Place" />
          <button type="submit">search</button>
 </form>

关于使用regExp,您可能应该使用new RegExp()test()exec()

More info on regExp

<script type="text/javascript">
var reg=new RegExp("^[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}$","g");
var chaine1="15/12/2017";
var chaine2="1a/bb/2017";
document.write(chaine1+" ");
if (reg.test(chaine1)) {document.write("est bien au format date<BR>")}
else {document.write("n'est pas au format date<BR>")}
document.write(chaine2+" ");
if (reg.test(chaine2)) {document.write("est bien au format date")}
else {document.write("n'est pas au format date")}
</script>

答案 2 :(得分:0)

我已经通过replace()解决了我的问题(在DOM中)

function validateMyForm(){
regex = /[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/g;
$('#formGroupExampleInput1').val($('#formGroupExampleInput1').val().replace(regex,' '));
$('#formGroupExampleInput2').val($('#formGroupExampleInput2').val().replace(regex,' '));}

我用纯文本替换了所有不允许的字符,并以他的方式防止注入脚本!

相关问题