Solved

Searching the table with the given text

Posted on 2006-11-01
27
162 Views
Last Modified: 2012-08-13
I am trying to get the results which match the given text in the textbox field from the table

for example:  if i give 'United States' or 'UNITED STATES' or 'united states', i am able to get a record from the table.

But if i give 'united' or 'states' or any combination in that string.  I do not get any record.

Is there any way to check to see if the table 'contains' the match

here is the snippet:
function convert(txtStr)
{
     var tmpName = txtStr.toLowerCase();
     var result  = "";
     var flag = 0;
     for (var i = 0; i < tmpName.length; i++)
     {
          if(i == 0){
              result += tmpName.charAt(i).toUpperCase()
              continue;
          }
           if (tmpName.charAt(i) == " "){
               result += tmpName.charAt(i)
                flag = 1;
               continue;
          }
          if(flag == 1){
              result += tmpName.charAt(i).toUpperCase();
              flag = 0;
          }
          else
              result += tmpName.charAt(i);
      }
      return result;
}

if (frmMain.txtName.value)
{
     var temp = convert(frmMain.txtName.value);
     query += "@Name='" + temp + "'";
}

any help would be greatly appreciated
anagm
0
Comment
Question by:anagm
  • 12
  • 11
  • 3
  • +1
27 Comments
 
LVL 9

Expert Comment

by:owenli27
ID: 17850244
Anagm

Does "like" work for you?
if (frmMain.txtName.value)
{
     var temp = convert(frmMain.txtName.value);
     query += "@Name like '%" + temp + "%'";
}
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17850294
you cannot search database with javascript
0
 
LVL 15

Expert Comment

by:dave4dl
ID: 17850390
what you are looking for is the indexOf function.  This seaches through the string and returns the character position at which the string to search for is found (if it is found)

see http://wsabstract.com/javatutors/string4.shtml
0
 

Author Comment

by:anagm
ID: 17850401
It gives me an error:

i am searching by text 'United'

msxml3.dll: Expected token ']' found 'NAME'.

//Property[@Name -->like<-- '%United%' and @Language='English']
0
 

Author Comment

by:anagm
ID: 17850422
YZlat, i am searching the table
0
 

Author Comment

by:anagm
ID: 17850468
dave4dl, how should i use indexof function with my existing code
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17850511
your query should be something like that:

"//Property[contains(@Name,temp)]"
0
 
LVL 15

Expert Comment

by:dave4dl
ID: 17850569
you should loop through your array of names (assuming you maintain an array) comparing each item to your target text using indexOf.

If you are running a query against a database from your javascript you should use the "LIKE" keyword with wildcards.  If the query is giving you an error, make sure final query it builds actually runs in whatever database system you are using.
0
 

Author Comment

by:anagm
ID: 17850668
can you please provide me with an example or a modification to my existing code.

0
 

Author Comment

by:anagm
ID: 17850809
YZLat,  

contains?

Should i use XPATH function: contains?

can you provide me with an example, please
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17850879
yes. the example is above
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17850887
Comment from YZlat
Date: 11/01/2006 08:12AM PST
ID: 17850511 Your Comment  


your query should be something like that:

"//Property[contains(@Name,temp)]"
0
 

Author Comment

by:anagm
ID: 17851453
the snipped to query the tabe is as follows:

var xpathQuery = "//Property"
if (query)
{
xpathQuery += "[" + query + "]";
}

how should i modify this?  YZLat, please help
                        
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 35

Expert Comment

by:YZlat
ID: 17851556
something like this

var xpathQuery = "//Property"
query="contains(@Name,temp)"
if (query)
{
xpathQuery += "[" + query + "]";
}

0
 
LVL 35

Expert Comment

by:YZlat
ID: 17851570
actually

 var(xpathQuery = "//Property")
        query = "contains(@Name," + temp + ")"
        If (query) Then
{
xpathQuery += "[" + query + "]";
}
0
 

Author Comment

by:anagm
ID: 17852393
YZlat,

function convert(txtStr)
{
     var tmpName = txtStr.toLowerCase();
     var result  = "";
     var flag = 0;
     for (var i = 0; i < tmpName.length; i++)
     {
          if(i == 0){
              result += tmpName.charAt(i).toUpperCase()
              continue;
          }
           if (tmpName.charAt(i) == " "){
               result += tmpName.charAt(i)
                flag = 1;
               continue;
          }
          if(flag == 1){
              result += tmpName.charAt(i).toUpperCase();
              flag = 0;
          }
          else
              result += tmpName.charAt(i);
      }
      return result;
}

if (frmMain.txtName.value)
{
     var temp = convert(frmMain.txtName.value);
     query += "@Name='" + temp + "'";
}

var xpathQuery = "//Property"
query = "contains(@Name," + temp + ")"
If (query)
{
xpathQuery += "[" + query + "]";
}

I get an error at (query = "contains(@Name," + temp + ")"

error message:  'Object expected'

need some more help please

0
 

Author Comment

by:anagm
ID: 17852446
actually, it is as follows;

function convert(txtStr)
{
     var tmpName = txtStr.toLowerCase();
     var result  = "";
     var flag = 0;
     for (var i = 0; i < tmpName.length; i++)
     {
          if(i == 0){
              result += tmpName.charAt(i).toUpperCase()
              continue;
          }
           if (tmpName.charAt(i) == " "){
               result += tmpName.charAt(i)
                flag = 1;
               continue;
          }
          if(flag == 1){
              result += tmpName.charAt(i).toUpperCase();
              flag = 0;
          }
          else
              result += tmpName.charAt(i);
      }
      return result;
}


function earch()
{
var query = "";
if (frmMain.txtName.value)
{
     var temp = convert(frmMain.txtName.value);
     query += "@Name='" + temp + "'";
}

var xpathQuery = "//Property"
query = "contains(@Name," + temp + ")"
If (query)
{
xpathQuery += "[" + query + "]";
}
}

I get an error at (query = "contains(@Name," + temp + ")"

error message:  'Object expected'

need some more help please
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17852598
this part

if (frmMain.txtName.value)
{
     var temp = convert(frmMain.txtName.value);
     query += "@Name='" + temp + "'";
}

var xpathQuery = "//Property"
query = "contains(@Name," + temp + ")"
If (query)
{
xpathQuery += "[" + query + "]";
}

should be


if (frmMain.txtName.value)
{
     var temp = convert(frmMain.txtName.value);
     query += "contains(@Name," + temp + ")"
}

var xpathQuery = "//Property"
If (query)
{
xpathQuery += "[" + query + "]";
}


0
 
LVL 15

Expert Comment

by:dave4dl
ID: 17852648
ah, i should have recognized it.  I didnt realize you were using xpath.
0
 

Author Comment

by:anagm
ID: 17852742
I tried like that i get the following error:

msxml3.dll: Unknown method.
//Property[-->contains(@<--Name,United) and @Language='ENGLISH']

what modification i should do?
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17852810
try

if (frmMain.txtName.value)
{
     var temp = convert(frmMain.txtName.value);
     query += "contains(@Name,'" + temp + "')"
}

var xpathQuery = "//Property"
If (query)
{
xpathQuery += "[" + query + "]";
}
0
 

Author Comment

by:anagm
ID: 17852860
i get an error like this:

//Property[-->contains(@<--Name,'United') and @Language='ENGLISH']
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17852892
0
 

Author Comment

by:anagm
ID: 17853841
I am trying like this:

if (frmMain.txtName.value)
{
query += "contains(translate(@Name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'))='" + frmMain.txtName.value.toLowerCase() +"'";
}

the contains() seems to work, but when i include the translate,

it gives me the error saying

msxml3.dll: Expected token ',' found ')'.

//Property[contains(translate(@Name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')-->)<--='united' and @Language='ENGLISH']

am i missing something in the code?

need some help
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17858383
it should be

if (frmMain.txtName.value)
{
query += "contains(translate(@Name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'), frmMain.txtName.value.toLowerCase() )";}
0
 
LVL 35

Accepted Solution

by:
YZlat earned 500 total points
ID: 17858404
translate(@Name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')
is the first argument of CONTAINS and
frmMain.txtName.value.toLowerCase() is the second. There is gotta be a comma between those and not a parenthesis
0
 

Author Comment

by:anagm
ID: 17918631
thank you, YZlat
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

706 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

18 Experts available now in Live!

Get 1:1 Help Now