Contains in jQuery

Suppose you want to check whether a string contains a particular sub string  or not and want to perform other action according to this result then you can easily do that using jQuery simple code which I will show in this Blog.
 
I am explaining this using a simple e.g. where I will check whether our sting contains a particular prefix or not, if not then that prefix will be append.
  1. $('#chatText').keyup(function () 
  2. {  
  3.   var prefix = 'Me:';  
  4.   if (!(this.value.match('^Me:'))) 
  5.   {  
  6.      this.value = prefix + this.value;  
  7.   }  
  8. }); 
Here I am checking the string entered in a particular textbox is having "Me:" as it's prefix or not, if not then it's appended.
 
For checking the string two main things are used. First is ".match" and Second is "^" sign. These two works together and give us the output as true or false.
Ebook Download
View all
Learn
View all