need help with something

A

ATeal75

I'm a college student, and I'm having trouble with one of my
assignments. We creating a spell checker in javascript that has to
compare a user submitted word (using forms) to a 5 word dictionary. If
the word is spelled correctly, all we have to do is say so. If not, we
have to suggest a word from the dictionary based on the individual
letters of the word, or ask if the user wants to add the word to the
dictionary, which would incrememnt the dictionary to 6 words as long as
the browser remains open.

I'm not asking for anyone to tell me how to do this, nor am I asking
anyone to do it for me. I'm simply asking if someone could look at
what I have so far and tell me what I'm doing wrong. I'm a long way
from being done, as I've just started getting stuff to work for me.
I'm very limited in my javascript ability, as the pre-requisite class
for this one was a joke.

Here's what I have so far. What I'm trying to do is take the word the
user types in the first text box and when they press the submit button,
it shows up in the second text box:

<script type="text/javascript">

function usersubmit (form){
var x = document.submitform.userentry.value;
document.userWord.userWordResult.value = x;
}
-->
</script>
</head>
<body>

<form name=submitform onsubmit="return checkform(this);">
<input type=text name=userentry>
<input type=submit value="Submit" >
</form>

<form name=userWord onsubmit="return usersubmit(this);">
<input type=text name=userWordResult>
</form>
 
L

Lee

Danny said:
The event handler onsubmit on form "submitform" should have the
.value assigmening function and should do it before
returning/submitting the form for processing:

function checkform(form) {
........
var x = document.submitform.userentry.value;
document.userWord.userWordResult.value = x;
form.submit();
}

An onsubmit handler should probably never contain form.submit().
In this particular case, you don't want to submit anything, anyway,
and so shouldn't be using a submit button at all.

Replace the submit button with simple:
<input type="button"
value="spell check"
onclick="checkform(this.form)">

Then there's also no reason to put it in a separate form, in
which case your checkform() function can become:

function checkform(f) {
var x = f.userentry.value;
f.userWordResult.value = x;
}
 
A

ATeal75

Thanks for the help. I was able to get that working, now I'm trying to
compare the word submitted by the user to the Array I created. This
code, while I know it's not quite there yet, keeps giving me a "Form
has no properties" error:

<script type="text/javascript">
<!--
function readText (form) {
var wordList = new Array("their", "calendar", "cemetery",
"recommend", "believe");
i = form.inputbox.value;
var wordUsed = form.inputbox.value;
/*var wordSplit = wordUsed.split("");*/
form.userWordResult.value = "";

if(wordList == wordUsed)
form.userWordResult.value = "Correct";
}

-->
</script>
</head>
<body onLoad="readText()">
<form name="myform" method="GET">
Enter your word: <br />
<input type="text" name="inputbox">
<input type="button" name="button2" Value="Check Spelling"
onClick="readText(this.form)">
<p>Your word is:</p>
<input type="text" name="userWordResult"
onClick="writeText(this.form)">
</form>


Any ideas?
 
V

VK

(e-mail address removed) wrote:


Your form is called "myform", not "form"

// Add here:

var form = document.forms['myform'];

// the rest as it is. I don't agree with the syntacs, but it makes the
trick:
 
A

ATeal75

Thanks. Now if I could just get it to tell me if the word I entered
was correct or not, I'd be all set.

Thanks again.

Al
 
L

Lee

VK said:
(e-mail address removed) wrote:


Your form is called "myform", not "form"

// Add here:

var form = document.forms['myform'];

That's not actually the problem.

The readText() function expects to have a reference to
the form passed to it as an argument. You do that
correctly in the button's onclick handler, but for some
reason, you're also calling readText() from the body's
onload handler, and in that case, you're not passing a
reference to the form. I don't know why you would call
the function on load, anyway, since there is no value to
be checked.

You also try to call readText() from the onclick handler
of the userWordResult field, but text input fields don't
have onclick handers.
 
A

ATeal75

OK, I think I see what you're talking about. I tried this, but when I
click on the button, nothing happens:

<script type="text/javascript">
<!--
function readText (form) {
var wordList = new Array("their", "calendar", "cemetery",
"recommend", "believe");
i = form.inputbox.value;
var wordUsed = form.inputbox.value;
/*var form = document.forms['myform'];
var wordSplit = wordUsed.split("");*/
form.userWordResult.value = "";

if(wordList == wordUsed)
document.writeln = "Correct";
}

-->
</script>
</head>
<body>
<form name="form" method="GET">
Enter your word: <br />
<input type="text" name="inputbox">
<input type="button" name="button2" Value="Check Spelling"
onClick="readText(this.form)">
<p>Your word is:</p>
<input type="text" name="userWordResult"
onClick="writeText(this.form)">
</form>
 
A

ATeal75

OK, this is what I have so far. Now I just need to get it to check for
instances of ddouble typed letters and imsplaced letters.

<script type="text/javascript">
<!--
function readText (form)
{
var wordList = new Array()
wordList[0] = "their"
wordList[1] = "calendar"
wordList[2] = "cemetery"
wordList[3] = "recommend"
wordList[4] = "believe"
i = form.inputbox.value;
var wordUsed = form.inputbox.value;
var i;

for(i=0;i<wordList.length;i++)
{
if(wordUsed == wordList){
alert('Your word is spelled correctly');
break;
}
else if(wordUsed != wordList){
alert('Your word is NOT spelled correctly');
}
}
//if (wordUsed != wordList)
// user wants to add word to array:
// wordList.push(wordUsed)
wordList.push(wordUsed)
form.arrayObjects.value = (wordList.join(", "));
form.userWordResult.value = i.length + ", " + wordUsed.split("");

}
-->
</script>
</head>
<body>
<form name="form" method="GET">
Enter your word: <br />
<input type="text" name="inputbox">
<input type="button" name="button2" Value="Check Spelling"
onClick="readText(this.form)">
<p>Your word is:</p> <p>
<input type="text" name="userWordResult"
onClick="writeText(this.form)">
</p>
<p>
<textarea name="arrayObjects" cols="60" rows="6"
id="arrayObjects"></textarea>
</p>
<p></p>
<input type="reset" name="Reset" value="Clear All fields" />
</form>
 
R

RobG

OK, this is what I have so far. Now I just need to get it to check for
instances of ddouble typed letters and imsplaced letters.

To do that, you'll have to work out a good comparison function - do you
have any sort of algorithm worked out yet?
<script type="text/javascript">
<!--

There is no need for comment delimiters, just don't use them.

function readText (form)
{
var wordList = new Array()
wordList[0] = "their"
wordList[1] = "calendar"
wordList[2] = "cemetery"
wordList[3] = "recommend"
wordList[4] = "believe"
i = form.inputbox.value;

Here you give 'i' a value in a global context...

All the above statements should be terminated with semi-colons: they
aren't strictly necessary in the source code but they're preferred.

Check out initializers and array literals:

Object initializer (general case):
<URL:http://devedge-temp.mozilla.org/library/manuals/2000/javascript/1.5/guide/obj.html#1008330>

Array literal (a kind of initializer):
<URL:http://devedge-temp.mozilla.org/library/manuals/2000/javascript/1.5/guide/ident.html#1011655>


var wordUsed = form.inputbox.value;
var i;

Now you declare a local 'i'.

for(i=0;i<wordList.length;i++)

And now set the local 'i' to zero to use it in the for loop. Checkout
the for loop syntax at devedge:

{
if(wordUsed == wordList){
alert('Your word is spelled correctly');
break;
}
else if(wordUsed != wordList){
alert('Your word is NOT spelled correctly');


You will tell a user that their word isn't spelled correctly even though
it might match one later in the list?

Your logic should be that you suspect that the word it is incorrectly
spelled only if none of the words match (and don't pester the user with
alerts in the meantime). :)

}
}
//if (wordUsed != wordList)
// user wants to add word to array:
// wordList.push(wordUsed)
wordList.push(wordUsed)
form.arrayObjects.value = (wordList.join(", "));
form.userWordResult.value = i.length + ", " + wordUsed.split("");

I'll guess that what you are going to do here is: if no matches are
found, ask the user if they'd like to add the word to the list of
allowed words. I don't think you should just add the word without asking.

}
-->
</script>
</head>
<body>
<form name="form" method="GET">
Enter your word: <br />
<input type="text" name="inputbox">
<input type="button" name="button2" Value="Check Spelling"
onClick="readText(this.form)">
<p>Your word is:</p> <p>
<input type="text" name="userWordResult"
onClick="writeText(this.form)">

If you really are writing XHTML, attribute names must be in lower case.
But I suspect you aren't, so drop the XHTML closing tags '/>' for
empty elements.

</p>
<p>
<textarea name="arrayObjects" cols="60" rows="6"
id="arrayObjects"></textarea>
</p>
<p></p>
<input type="reset" name="Reset" value="Clear All fields" />
</form>

Make sure you validate your HTML too, it can make a difference (though
in this case it's just for tidiness). The W3C validator takes either a
URL, file upload or pasted code.

<URL:http://validator.w3.org/>
 
A

ATeal75

Awesome! Thanks a lot, I really appreciate the help. I have until
Friday, so I'll let you know how I make out.

Al
 
A

ATeal75

By the way, this is what I've done since my last post:

<script type="text/javascript">
<!--
function readText (form)
{
var wordList = new Array()
wordList[0] = "their"
wordList[1] = "calendar"
wordList[2] = "cemetery"
wordList[3] = "recommend"
wordList[4] = "believe"
wordList[5] = ""
i = form.inputbox.value;
var wordUsed = form.inputbox.value;
var i;

for(i=0;i<wordList.length;i++)
{
if(wordUsed == wordList)
{
form.wordCompareResult.value = "Your word is spelled
correctly";
break;
}
else if(wordUsed != wordList)
{
form.wordCompareResult.value = "Your word is NOT spelled
correctly, or is NOT in the dictionary. To add it to the dictionary,
press the \'Add Word\' button.";
}
}
}
-->
</script>
</head>
<body>
<form name="form" method="GET">
Enter your word: <br />
<input type="text" name="inputbox">
<input type="button" name="button2" value="Check Spelling"
onClick="readText(this.form)">
<input type="submit" name="Submit" value="Add Word"
onclick="wordUsed.join(wordList)" />
<p>
<textarea name="wordCompareResult" cols="60" rows="6"
id="arrayObjects"></textarea>
</p>
<p>
<input type="reset" name="Reset" value="Clear All fields" />
</p>
</form>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top