Click here to Skip to main content
14,732,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My string messege contain the string either this:
var strMessage = "Plcc-failed|Score-passed|bankruptcy-passed|Years in Business
i.e. it contains a " | " symbol in it or this
var strmsg="fail".
I need a javascript function to check whether it contains a "fail" msg or " | " symbol.
How to write a javascript function for it.

Thanks in advance
Posted

You can use the indexOf method on the string.

var strMessage = "Plcc-failed|Score-passed|bankruptcy-passed|Years in Business"
var intIndexLocation = strMessage.indexOf("fail");
if (intIndexLocation >= 0)
{
  alert("fail is in the string");
}
   
You have to do small change to RyanDev's solution, "fail" is there in both the case and if condition will return true for both strings so you have to find the index of | instead.

var strMessage = "Plcc-failed|Score-passed|bankruptcy-passed|Years in Business";
var intIndexLocation = strMessage.indexOf("|");
if (intIndexLocation >= 0)
{
  alert("| is in the string");
}
   
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900