Solved

Javascript equivalent of @contains

Posted on 2000-04-12
4
404 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

I thought it will be a good idea to make a post as it will help in case someone else faces these issues. I trust this gives an idea how each entry in Notes.ini can mean a lot for the Domino Server to be functioning properly. This article discusses t…
Notes Document Link used by IBM Notes is a link file which aids in the sharing of links to documents in email and webpages. The posts describe the importance and steps to create a Lotus Notes NDL file in brief.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now