ngplayground ngplayground - 1 year ago 236
jQuery Question

jQuery detect if string contains something

I'm trying to write jQuery code to detect if a live string contains a specific set of characters then the string alerts me.


<textarea class="type"></textarea>

My Jquery

$('.type').keyup(function() {
var v = $('.type').val();
if ($('.type').is(":contains('> <')")){
console.log('contains > <');

if for example I typed the following

> <a href="">Google</a> <a href="">Yahoo</a>

My code should console log alert me that there > < present in the string.

Answer Source

You could use String.prototype.indexOf to accomplish that. Try something like this:

$('.type').keyup(function() {
  var val = this.value;
  if ( val.indexOf('> <') !== -1 ) {
    console.log('contains ', val);        

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download