这个页面说NaN

时间:2016-12-30 14:27:58

标签: javascript validation nan

当我的JS调用时,它会一直返回NaN

我的两个职能是: 确保某些字段已完成 确保跑步者和赛事ID在1 - 9999之间

但是当它运行时它只是提醒我NaN

谢谢,如果有人可以提供帮助

<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=iso-8859-1"/>
<title>Submit a runner time</title>
</head>
<body>
<hr/>
<h1>Submit a runner time</h1>
<hr/>
Note: all fields marked '*' are mandatory.
<p/>
<form action="http://students.open.ac.uk/mct/tt284/reflect/reflect.php"
      method="post" name="submitrunnertime">
<table>
<tr><td>Runner ID*</td>
<td><input type="text" id = 'RunnerID' name="RunnerID" size="4" maxlength="4"/></td>
</tr>
<tr><td>Event ID*</td>
<td><input type="text" id="EventID" name="EventID" size="4" maxlength="4"/></td>
</tr>
<tr><td>Date (YYYY-MM-DD)*</td>
<td><input type="text" id="Date" name="Date" size="10" maxlength="10"/></td>
</tr>
<tr><td>Finish time (HH:MM:SS)*</td>
<td><input type="text" id="Finish" name="FinishTime" size="8" maxlength="8"/></td>
</tr>
<tr><td>Position</td>
<td><input type="text" name="Position" size="5" maxlength="5"/></td>
</tr>
<tr><td>Category ID</td>
<td><input type="text" name="CategoryID" size="3" maxlength="2"/></td>
</tr>
<tr><td>Age grade</td>
<td><input type="text" name="AgeGrade" size="5" maxlength="5"/></td>
</tr>
<tr><td>Personal best</td>
<td><input type="text" id="PB" name="PB" size="1" maxlength="2"/></td>
</tr>
</table>
<input  type="submit" name="submitrunnertime" value="submit" onclick="return validate();"/>
<hr/>
</form>

<script type="text/javascript">


function isNotEmptyValue(value,name)
{

   var id = document.getElementById(value).value;

  if (id == null || id == "")
  {
    return "ERROR " + name + " must be given\n";

    return false;

  }
}


function lengthCheck(value, min, max)
{

  var x = document.getElementById('RunnerID').value;

  console.log("Length check test");
  if (parseInt(x) < min || parseInt(x) > max)
  {
    return "Please ensure that the number you input is between 1 and 9999 \n";
    return false;

  }
}

// main function to run on sumbit
function validate()
{

  var result = isNotEmptyValue('RunnerID','RunnerID') + isNotEmptyValue('EventID',' EventID')
              + isNotEmptyValue('Date','Date') + isNotEmptyValue('Finish','Finish') +
              lengthCheck('RunnerID',1,4) + lengthCheck('EventID',1,4);

  if(result != null || result != "")
  {
    alert(result);
    return false;
  }
}

</script>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

如果测试没问题,你忘了退货:

function isNotEmptyValue(value,name)
{
   var id = document.getElementById(value).value;
   if (id == null || id == "")
   {
       return "ERROR " + name + " must be given\n";
   }
   return ''; 
}