468,110 Members | 1,579 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,110 developers. It's quick & easy.

need help with something

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>

Feb 11 '06 #1
10 1276


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();
}

Dann
Feb 11 '06 #2
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[i] == 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?

Feb 11 '06 #3
VK

AT*****@gmail.com 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:
i = form.inputbox.value;
var wordUsed = form.inputbox.value;
/*var wordSplit = wordUsed.split("");*/
form.userWordResult.value = "";

if(wordList[i] == wordUsed)
form.userWordResult.value = "Correct";


Feb 11 '06 #4
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

Feb 12 '06 #5
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[i] == 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>

Feb 12 '06 #6
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[i]){
alert('Your word is spelled correctly');
break;
}
else if(wordUsed != wordList[i]){
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>

Feb 14 '06 #7
AT*****@gmail.com wrote:
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:

<URL:http://devedge-temp.mozilla.org/library/manuals/2000/javascript/1.5/guide/stmtsov.html#1008347>

{
if(wordUsed == wordList[i]){
alert('Your word is spelled correctly');
break;
}
else if(wordUsed != wordList[i]){
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/>

--
Rob
Feb 14 '06 #8
Awesome! Thanks a lot, I really appreciate the help. I have until
Friday, so I'll let you know how I make out.

Al

Feb 14 '06 #9
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[i])
{
form.wordCompareResult.value = "Your word is spelled
correctly";
break;
}
else if(wordUsed != wordList[i])
{
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>

Feb 14 '06 #10
Did you ever get this thing working? I'd like to see what you ended up
with. Thanks.

Feb 25 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Confused User | last post: by
7 posts views Thread by Jack Addington | last post: by
7 posts views Thread by The Cool Giraffe | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.