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!
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.
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 .
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With