Solved

Javascript equivalent of @contains

Posted on 2000-04-12
4
419 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

You’ve got a lotus Domino web server, and you have been told that “leverage browser caching” is a must do. This means that we have to tell the browser everywhere in the web to use cache. In other words, we set (and send) an expiration date in the HT…
For beginners of Lotus Notes user this is important to know about the types of files and their location supported by IBM Notes. Mostly users are unaware about how many file types are created and what their usages are. This Article is fully dedicated…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

726 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