I have a program where the user enters a sentence and the input is compared against an array when the user clicks the submit button the function "checkSentence() should be called but this does not happen. would really appreciate it if someone told me what's wrong wth my code

<script type="text/javascript">
var noun=["cat","mat","rat"];
var verb=["like","is","see","sees"];
var object=["me","the","a"];
var subject=["I","a","the"];
var sentence=subject.concat(verb,noun,object);
var userInput=document.getElementById("idTextArea").value;
function compareSentences(){
    if (userInput==sentence.length){
    alert("congratulations! Your sentence is correct");
    return false;
    }
    else if(userInput==null||userInput=="");
           {
           alert("your text was blank, please enter a valid sentence")
           }

    else
    alert("Your Sentence does not follow micro-english rules,Please enter a micro-English sentence in the form of a subject, verb and object");

    return false;

}
</script>


<body>
<div id="header">
<img src=http://www.myfreebannercreator.com/banners/4092135978862643.png alt="logo">
<br/>
<h1><font face="comic sans ms">Micro-English</font></h1>
</div>



<h1><font size="3" color="black" face="comic sans ms">Welcome to Micro English</font></h1>
<p>Micro English is a mini version of English with a limited vocabulary. You may only use the following</p>
<dl>
<dt>Nouns</dt>
<dd>Cat, Mat,Rat</dd>
<dt>Verbs</dt>
<dd>Like,is,see,sees</dd>
<dt>objects</dt>
<dd>me,the,a</dd>
<dt>Subjects</dt>
<dd>I,a,the</dd>
</dl>
<h2><font size="2" color="blue" face="comic sans ms">Please Enter a sentence in micro English in the box below</font></h2>

<form onsubmit="checkSentences();">
<p><font face="tempus sans itc">Please Enter a sentence in micro English</font></p>
<input type="text" name="input" id="userInput"/>
<input type="submit" value="go" onclick="compareSentences();"/>

</form>

You have reference to two different functions. I only see this function:

 function compareSentences()

I don't see a function called checkSentence().

Modified that thanks. but the function call still won't happen

Member Avatar for Rahul47

Modified that thanks. but the function call still won't happen

Post your modified code.

You have all sorts of syntax errors and logic problems in your code. here is an example where the function will run when you click on submit.

<!DOCTYPE html>
<html>
<head>
<script>
var noun=["cat","mat","rat"];
var verb=["like","is","see","sees"];
var object=["me","the","a"];
var subject=["I","a","the"];
var sentence=subject.concat(verb,noun,object);
var userInput;
function compareSentences(){
    userInput = document.getElementById("userInput").value;
    if (userInput==sentence.length){
    alert("congratulations! Your sentence is correct");
    return false;
    }
    else if(userInput==null||userInput=="")
           {
           alert("your text was blank, please enter a valid sentence");
           }
    else {
    alert("Your Sentence does not follow micro-english rules"...);
    return false;
}
return false;
}
</script>
</head>
<body>
<div id="header">
<img src=http://www.myfreebannercreator.com/banners/4092135978862643.png alt="logo">
<br/>
<h1><font face="comic sans ms">Micro-English</font></h1>
</div>
<h1><font size="3" color="black" face="comic sans ms">Welcome to Micro English</font></h1>
<p>Micro English is a mini version of English with a limited vocabulary. You may ... </p>
<dl>
<dt>Nouns</dt>
<dd>Cat, Mat,Rat</dd>
<dt>Verbs</dt>
<dd>Like,is,see,sees</dd>
<dt>objects</dt>
<dd>me,the,a</dd>
<dt>Subjects</dt>
<dd>I,a,the</dd>
</dl>
<h2><font size="2" color="blue" face="comic sans ms">Please Enter a sentence ...</font></h2>
<form onsubmit="return compareSentences();">
<p><font face="tempus sans itc">Please Enter a sentence in micro English</font></p>
<input type="text" name="input" id="userInput"/>
<input type="submit" value="go" />
</form>
</body>
</html>

However, with regard to this condition if (userInput==sentence.length) , I dont undertand how you expect for that condition to ever be true.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.