PDA

View Full Version : Javascript Search Engine



mw2005
Aug 29th, 2005, 05:59 PM
I have created a Javascript search engine. I am having a bit of trouble with one thing. If somebody enters some text which isn't there the results go blank. I have tried to use the != funtion but yet had no success.

Please can you show me what i need to do,
Any help will be appreciated,
Thanks in Advance,
MW2005


<script type="text/javascript">

function validate(f) {
var retVal = true;



document.getElementById('question').innerHTML="";
document.getElementById('answer').innerHTML="";


if(f.value.value == "number" || f.value.value == "Pages" || f.value.value == "pages" ) {
retVal = false;
document.getElementById('question').innerHTML='How many pages are there in this website? ';
document.getElementById('answer').innerHTML='Around 500 pages.';
}

if(f.value.value == "" ) {
retVal = false;
document.getElementById('answer').innerHTML='Please enter some search criteria';
}


return retVal;
}

</script>



<form name="myForm" onsubmit="return validate(this)">

<p align="center">

<input type="text" name = "value" id="value" value=""/><input type="submit" value="search" id="submitbutton"/>
<br /><br /><br />
<span id="question"></span><br />
<span id="answer"></span>
</p>

</form>

Mr J
Aug 29th, 2005, 09:01 PM
Try adding

else{
retVal = false;
document.getElementById('answer').innerHTML='Please enter some search criteria';
}

to the first if statement

mw2005
Aug 29th, 2005, 09:06 PM
Thanks for that. I have Microsoft Frontpage and when i try previewing it it came up with two errors. Is this because i am viewing it offline?

_Aerospace_Eng_
Aug 29th, 2005, 09:32 PM
See if this gets the results you want.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">

function validate(f) {
if(f.value1.value == "number" || f.value1.value == "Pages" || f.value1.value == "pages" ) {
document.getElementById('question').innerHTML='How many pages are there in this website? ';
document.getElementById('answer').innerHTML='Around 500 pages.';
}
else if(f.value1.value == "" ) {
document.getElementById('question').innerHTML='';
document.getElementById('answer').innerHTML='Please enter some search criteria';
}
return false;
}

</script>
</head>

<body>
<form onsubmit="return validate(this)">

<p align="center">

<input type="text" name = "value1" id="value1" value=""/><input type="submit" value="search" id="submitbutton"/>
<br /><br /><br />
<span id="question"></span><br />
<span id="answer"></span>
</p>

</form>
</body>
</html>
I changed the name of your input to something other than value since value is a reserved word in javascript.

Mr J
Aug 29th, 2005, 11:56 PM
With the following if you enter anything other than the words "numbers" or "pages" the message "Please enter some search criteria" is shown




<script type="text/javascript">
<!--
function validate(f) {
var retVal = true;

document.getElementById('question').innerHTML="";
document.getElementById('answer').innerHTML="";

if( f.value.value != "number" && f.value.value.toLowerCase() != "pages") {
retVal = false;
document.getElementById('answer').innerHTML='Please enter some search criteria';
}
else{
retVal = false;
document.getElementById('question').innerHTML='How many pages are there in this website? ';
document.getElementById('answer').innerHTML='Around 500 pages.';
}


return retVal;
}
//-->
</script>

<form name="myForm" onsubmit="return validate(this)">

<p align="center">

<input type="text" name = "value" id="value" value=""/><input type="submit" value="search" id="submitbutton"/>
<br /><br /><br />
<span id="question"></span><br />
<span id="answer"></span>
</p>

</form>



On a side note you should not really use the word value for names or IDs, as you can see this word is already used within the input to denote the default value of the text box and may throw up some confusion.
I think it is considered a reserved word.

mw2005
Aug 30th, 2005, 10:05 AM
Thanks for both of your help, it now works :thumbsup: