-1

I got a website need to check user input value is contain porn site URL or not, so I can do reject the input

Using: Chrome

$porn = [''];
$.getJSON('https://cdn.rawgit.com/aredo/porn-site-list/master/sites.json',function(data){
  $(data).each(function(index,value){
    $pornlist = value.host;
    $porn.push($pornlist);
  });

$('.urlinput').on('input',function(){
  $text = $(this).val();

  if (jQuery.inArray($porn,$text) != -1) {
    //Porn site not allow
    console.log('in webiste')
  } else {

    //Not porn site allow
      console.log('not in website')
  }

  });

});
6
  • 1
    If you don't need <IE9 support, Array.prototype.indexOf works fine as well. Commented Jun 29, 2016 at 12:11
  • 1
    if you just do a loop.. Commented Jun 29, 2016 at 12:12
  • I'm using Chrome,but still not working Commented Jun 29, 2016 at 12:12
  • I try stackoverflow.com/questions/18867599/… before,but still not working Commented Jun 29, 2016 at 12:13
  • 1
    Also, just personal opinion, these are not jQuery objects, so prefixing the variables with '$' is unnecessary. Commented Jun 29, 2016 at 12:17

5 Answers 5

2

Try:

$site = ['google','facebook','twitter'];
$text = 'facesbook';
if (jQuery.inArray($text, $site)  !== -1 ) {
    console.log('in webiste')
}else {
    console.log('not in website')
}
Sign up to request clarification or add additional context in comments.

1 Comment

An explanation of the problem helps to educate the user too,
2

jQuery.inArray($text, $site) >-1 is the key. When no match found -1 will be returned instead of the index.

Demo: http://codepen.io/8odoros/pen/aZJPgV

$site = ['google','facebook','twitter'];

//$('.urlinput').on('input',function(){
  //$text = $(this).val();
  $text = 'facebook';

  if (jQuery.inArray($text, $site) >-1) {
    console.log('in webiste')
  }else {
    console.log('not in website')
  }

// });

3 Comments

What does this question have to do with php?
An explanation of the problem helps to educate the user too,
@Felix, maybe jQuery is missing from that page then? It should work.
1

Your issue is comparing the result to true. $.inArray (as with indexOf) returns an integer value. -1 if the element is not found and it's index in the array if it is found. Therefore your logic would return true when -1 or > 0 and false if the element is found at index 0.

To fix this, compare to -1 instead:

if (jQuery.inArray($text, $site) != -1) {
    console.log('in webiste')
} else {
    console.log('not in website')
}

Comments

1

$.inArray($string, $array) returns the index of the element in the array, if it exists in the array. If the element was not found, -1 will be returned

$site = ['google', 'facebook', 'twitter'];
$text = 'facebook';
if ($.inArray($text, $site) !== -1) {
  console.log('in webiste')
} else {
  $.log('not in website')
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Comments

-1

You need to run the loop and test the value

for ( var i = 0; i < $site.length; i++){
 if ( $site[i] == $text) {
    console.log('in webiste')
  }else {
    console.log('not in website')
  }
}

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.