Wednesday, 1 November 2017

jQuery: Using contains in jQuery

jQuery doesn't have the "Contains" function like C# to check the existence of any special character or specific alphabet. To achieve this in jQuery we can use "indexOf". Check out the following example using "indexOf":

var data="test@gmail.com";
data.indexOf('@'); //Checking the existance of "@" in data.
Output: 4

data.indexOf('.'); //Checking the existance of "." in data.
Output: 10

data.indexOf('-');//Checking the existance of "-" in data.
Output: -1

Note: indexOf function always returns positive values if it contains the specified characters like in example for the values "." and "@" it returned 4 and 10. But for value "-" it returned negaive value "-1". In this way we can check the existance of specific alphabet or special character in jquery.

No comments:

Post a Comment