Comparison of value entered
<p>Trying to implement: if value entered into "year" field is not a number (NAN) javascript function - doesnt work. Is there an error in my syntax?</p>

<p>Thanks.</p>

<pre><code>&lt;?php

    echo "&lt;h1&gt;Testing your Trivia&lt;/h1&gt;";
    $ages['Casablanca'] = "1943";
    $ages['Around The World in 80 Days'] = "1956";
    $ages['Patton'] = "1970";
    $ages['Annie Hall'] = "1977";
    $ages['Chariots of Fire'] = "1981";
    $ages['Dances With Wolves'] = "1990";
    $ages['Crash'] = "2005";
    $ages['The Departed'] = "2006";

    $rand_keys = array_rand($ages, 1);

?&gt;
&lt;script language="JavaScript" type="text/javascript"&gt;

function validate ( form )
{
  var valid = "1234567890";
  if (form.year.value == "" || isNaN(valid) ) {
    alert( "Please enter a year." );
    return false;
  }
  return true;
}
&lt;/script&gt;

&lt;form method='post' name="inputyear" onsubmit="return validate(this);"&gt;
Give the year below won academy award&lt;br&gt;
&lt;Strong&gt;Movie:&lt;/strong&gt; &lt;input type='text' name='movie' value='&lt;?= $rand_keys ?&gt;'     readonly='readonly' /&gt;&lt;br&gt;
&lt;Strong&gt;Year it Won the Oscar:&lt;/Strong&gt; &lt;input type='text' name='year' size="30" /&gt;&lt;br/&gt;
&lt;input type='submit' name='submit' /&gt;
&lt;/form&gt;

&lt;?php

    echo '&lt;pre&gt;';
    foreach($ages as $movie =&gt; $year){
        print_r("Year: $year, Title: $movie &lt;br /&gt;");
    }
    echo '&lt;/pre&gt;';

    if($_POST['submit']) {
        $movie = $_POST['movie'];
        $year = $_POST['year'];
        $realyear = $ages[$movie];

        echo "&lt;h2&gt;Answer for: $movie&lt;/h2&gt;";
        if(array_key_exists($movie, $ages)) {
            echo "Your answer: $year&lt;br/&gt;";
        }
        echo "Correct Answer: $realyear&lt;br/&gt;";
        if ($realyear == $year) {
            echo "&lt;strong style='color:green;'&gt;WELL DONE&lt;/strong&gt;&lt;br/&gt;";
        } else {
        // wrong
            echo "&lt;strong style='color:red;'&gt;Incorrect.&lt;/strong&gt;&lt;br/&gt;";
        }
    }    

?&gt;
</code></pre>
