gpt4 book ai didi

javascript - 表单数据未验证

转载 作者:行者123 更新时间:2023-11-28 02:46:24 24 4
gpt4 key购买 nike

嘿,伙计们,我已经很久没有接触过 Java 了,发现我失去了大部分学到的东西。

尝试检查个人最佳成绩是否已被选中且值在 1 到 1000 之间

我一直在抓狂,希望你们能告诉我哪里出了问题,这样我就可以自己编写其他值。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="en-gb" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
<style type="text/css">
.auto-style1 {
width: 84%;
height: 156px;
}
.auto-style3 {
width: 286px;
}
.auto-style4 {
width: 151px;
}
.auto-style5 {
width: 123px;
}
</style>
<script type='text/javascript'>
function valueNumeric(){
var valueResult
var valuetaId=document.forms["myForm"]["Value"].value;
var numericExpression = /^[0-9]+$/;
if(valuetaId.match(numericExpression))
{
valuetaId = parseFloat(valuetaId)
if((valuetaId >= 1 ) ||(valuetaId <= 1000 ))
{
valueResult = ("Valid");
document.getElementById("vl").innerHTML=valueResult; }
else
{
valueResult = ("Please enter calue between 1 and 1000");
document.getElementById("vl").innerHTML=valueResult;
}
}else{
valueResult = ("Please enter calue between 1 and 1000");
document.getElementById("vl").innerHTML=valueResult;
}
}
function personalbest(selection)
{
var rundataResult;
var rundataId=document.forms["myForm"]["personalbest"].value;
if(rundataId == "Please Choose")
{
rundataResult = ("Please choose a value.");
document.getElementById("pb").innerHTML=rundataResult;
}
else{
rundataResult = ("valid");
document.getElementById("pb").innerHTML=rundataResult;
}
}
</script>
</head>

<body>

<form name="myForm" action="" method="post">
<br />
<table class="auto-style1">
<tr>
<td class="auto-style4">Value 1 to 1000</td>
<td class="auto-style5"><input name="Value" type="text" onblur="valueNumeric" /></td>
<td class="auto-style3">
<p id="vl">Please enter calue between 1 and 1000</p>
</td>
</tr>
<tr>
<td class="auto-style4">Date YY-MM-DD</td>
<td class="auto-style5"><input name="Date" type="text" /></td>
<td class="auto-style3">
<p>Please enter a date.</p>
</td>
</tr>
<tr>
<td class="auto-style4">Time HH-MM-SS</td>
<td class="auto-style5"><input name="Timme" type="text" /></td>
<td class="auto-style3">
<p>Please enter your time.</p>
</td>
</tr>
<tr>
<td class="auto-style4">Grade % i.e. 74.56</td>
<td class="auto-style5"><input name="Grade" type="text" /></td>
<td class="auto-style3">
<p style="width: 394px">Pease enter your grade as a % with 2 decimal
places</p>
</td>
</tr>
<tr>
<td class="auto-style4">Personal Best</td>
<td class="auto-style5"><select name="personalbest" onchange="personalbest('selection')">
<option selected="selected">Please Choose</option>
<option value="1">Yes</option>
<option value="0">No</option>
</select></td>
<td class="auto-style3">
<p id="pb">Please choose a value.</p>
</td>
</tr>
</table>
</form>

</body>

</html>

最佳答案

您没有调用valueNumeric使用onblur="valueNumeric()"调用它请注意 ()

还有if((valuetaId >= 1 ) ||(valuetaId <= 1000 ))将对任何值评估为 true,使用 if((valuetaId >= 1 ) && (valuetaId <= 1000 ))范围在 1 到 1000 之间

此外,与 personalbest 存在冲突元素名称和函数,因此将函数名称更改为 personalBest

FIDDLE

关于javascript - 表单数据未验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11788046/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com