Updated on Kisan Patel
This tutorial will show you how to check if string contains specific character using javascript?
To check if string contains specific character, we need to use javascript string indexOf()
method that returns the position of the first occurrence of a specified value in a string.
The indexOf()
method returns -1 if the value to search for never occurs.
<script type="text/javascript" > function TestFunction() { var str = "Hello world- welcome to the universe."; if(str.indexOf("e") === -1) { alert("No dash found!"); } else { alert("dash found!"); } } </script> <p>Click the button to find characters in a given string.</p> <button onclick="TestFunction()">Click me</button>
See the Pen zGbydQ by Kisan (@pka246) on CodePen.