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.
- $('#chatText').keyup(function ()
- {
- var prefix = 'Me:';
- if (!(this.value.match('^Me:')))
- {
- this.value = prefix + this.value;
- }
- });
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.