Link to home
Start Free TrialLog in
Avatar of snocross
snocross

asked on

Javascript equivalent of @contains

I have a field1 that contains multiple values. In field2 a web user selects a value.  I then validate the value in field2 to see if it is contained in field1 like this:

@If(!@Contains(field1;field2);@Failure("This option is not available."

I'm converting all my input validation formulas to Javascript and would like to know how to perform the equivalent of @contains using Javascript?
Avatar of HemanthaKumar
HemanthaKumar

Use this js command

indexOf  
=========

Method. Returns the index within the calling String  object of the first occurrence of the specified value, starting the search at fromIndex .  

 
 Syntax  


stringName.indexOf(searchValue, [fromIndex])
 
 Parameters  

stringName is any string or a property of an existing object.  

searchValue  is a string or a property of an existing object, representing the value to search for.  

fromIndex  is the location within the calling string to start the search from. It can be any integer from zero to stringName.length - 1 or a property of an existing object.  


eg:
var anyString="Brave new world"
//Displays 8
document.write("<P>The index of the first w from the beginning is " +
   anyString.indexOf("w"))
//Displays 10
document.write("<P>The index of the first w from the end is " +
   anyString.lastIndexOf("w"))
//Displays 6
document.write("<P>The index of 'new' from the beginning is " +
   anyString.indexOf("new"))
//Displays 6
document.write("<P>The index of 'new' from the end is " +
   anyString.lastIndexOf("new"))


Good Luck
~Hemanth
Avatar of snocross

ASKER

Ok, I think I understand the syntax but I'm not sure how to apply it to my situation... I'm not really looking to return a string or number value but instead a TRUE or FALSE.

Suppose field1 has the following values: "Cash":"Check":"Visa":"Mastercard"

Now suppose a user types "American Express" in field2.  I want to then check field1 to see if "American Express" exists otherwise tell them that this option is not available.
ASKER CERTIFIED SOLUTION
Avatar of melbor1
melbor1

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
so if you want to find the field value of field1 in field2....
var field1 = document.forms.field1.value
var field2 = document.forms.field2.value

var findString = field2.indexof( field1 )
if ( findString == -1 ) {
do this......
}

this is maybe a little more clear. :)
Melissa