Solved

Javascript equivalent of @contains

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

IBM Notes offer Encryption feature using which the user can secure its NSF emails or entire database easily. In this section we will discuss about the process to Encrypt Incoming and Outgoing Mails in depth.
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.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

816 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

10 Experts available now in Live!

Get 1:1 Help Now