Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Check if character is in string

Tags:

I want the simplest way to check if an underscore (_) is in a variable using jQuery and do something if is not..

if ( '_ is not in var') {    // Do } 

Thanks!

like image 384
Jonathan Avatar asked Jun 29 '10 09:06

Jonathan


People also ask

How do you check if a character is in a string JS?

Use the String. includes() method to check if a string contains a character, e.g. if (str. includes(char)) {} . The include() method will return true if the string contains the provided character, otherwise false is returned.

How check string is value or not in jQuery?

Answer: Use the jQuery. isNumeric() method You can use the jQuery $. isNumeric() method to check whether a value is numeric or a number. The $. isNumeric() returns true only if the argument is of type number, or if it's of type string and it can be coerced into finite numbers, otherwise it returns false .

How do you check if a variable contains a string in jQuery?

You can check using Javascript . IndexOf() if a string contains substring or not, it returns Index of substring if it is in a string otherwise it returns 0. console.


1 Answers

var str = "i am a string with _"; if (str.indexOf('_') == -1) {    // will not be triggered because str has _.. } 

and as spender said below on comment, jQuery is not a requirement.. indexOf is a native javascript

like image 98
Reigel Avatar answered Oct 22 '22 08:10

Reigel