Solved

Javascript equivalent of @contains

Posted on 2000-04-12
4
411 Views
Last Modified: 2013-12-18
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?
0
Comment
Question by:snocross
  • 2
4 Comments
 
LVL 24

Expert Comment

by:HemanthaKumar
ID: 2709464
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
0
 
LVL 5

Author Comment

by:snocross
ID: 2713278
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.
0
 
LVL 2

Accepted Solution

by:
melbor1 earned 40 total points
ID: 2713512
going on what was previously stated:

var stringLoc = stringName.indexof( searchValue , [fromIndex] )
if ( stringLoc == -1 ) {
do this
}
else {
do this
}

Basically if the string isn't found, then index of will return -1.

Hope this helps.
0
 
LVL 2

Expert Comment

by:melbor1
ID: 2713528
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
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an old article, please see an updated version of this article, located here: http://www.experts-exchange.com/articles/23619/Notes-8-5x-Windows-7-Notes-info-and-tips.html
Article by: Rob
Notes 8.5 Archiving Steps and Tips This article covers setting up a Notes archive, and helps understand some of the menu choices making setting up and maintaining a Notes archive file easier.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question