0

Trying to create a javascript method like below , where the input html will be scanned and log there if any of the entry in the list was found ..

why this specific because , we have to allow somemarkup like < or > or some html keywords , thats why

created this fiddle for this https://jsfiddle.net/ronqLes5/

JS

var input ;

$( "#target" ).click(function() {
 var identifier =false; 
 input=  $("#textbox1").val();
console.log(input.toLowerCase());
     var untrustedInput = ["script","jquery", "$", "Javascript","location","href"];
       for (var i = 0; i < untrustedInput.length ; i++) {
          if (input.toLowerCase().indexOf(untrustedInput[i].toLowerCase())> -1) {
         identifier =true;                
            }     
    }
    if(identifier){
    console.log("there");
    }
    else
    {
    console.log("not there");
    }
});
  1. would like to know if you have any faster and convenient way is there or any kind of suggestion.

  2. is there any more keywords you think i should enter it in the list.

3
  • 1
    You should probably have this function return true or some truthy value if the loop finishes without returning false. Commented Sep 6, 2017 at 15:27
  • I understand it's more "cost effective" to use indexOf to determine if the word is in the untrustedInput also @hello_world is right, your function should return false or true to keep the boolean logic... Commented Sep 6, 2017 at 15:37
  • <ScRiPt></ScRiPt> Commented Sep 6, 2017 at 15:52

1 Answer 1

1

Use only indexOf:

function CustomHtmlEndoder(input) {
    var untrustedInput = "<script>,</script>,jquery,$,Javascript,location,href";
    if (input.Indexof(untrustedInput)>=0) {
        return false;
    }      
    else{
       return true;
    }

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.