Solved

Searching the table with the given text

Posted on 2006-11-01
27
167 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
[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
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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
 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

707 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