-3
<!DOCTYPE html>
<html>
<head>
<script>
function validateAlphaNumeric()
{
alert("function start");
var alphabets="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var numbers="0123456789";
var flag=true;
var prod_id=document.getElementByID("productid").value;
alert(prod_id);
for(var i=0;i<prod_id.length;i++)
{
if(alphabets.indexOf(prod_id.charAt(i))==-1 || numbers.indexOf(prod_id.charAt(i)==-1)
{
alert("value must be alphanumeric");
break;
}
}


var prod_type=document.myform.producttype.value;
for(var i=0;i<prod_type.length;i++)
{
if(alphabets.indexOf(prod_type.charAt(i))==-1 || numbers.indexOf(prod_type.charAt(i)==-1)
{
alert("value must be alphanumeric");
break;
}
}
}
</script>
</head>
<body>

<form name="myform" action="http://localhost:8080/examples/Submit.HTML" method="POST">

<br><br>
Prodcut ID:<input type="text" name="productid" id="productid" size="25" ">
<br><br>
Product Type:<input type="text" name="producttype" id="producttype" size="25" ">
<br><br>
</div>

<input type="submit"  onclick="validateAlphaNumeric()" value="Submit">
</form>
</body>
</html> 

这是一个仅用于学习基础知识的测试页面。当我按下提交时,没有发生任何检查,它转到 Submit.html 页面。为什么?这里需要什么修改?

<input type="submit"  onclick="validateAlphaNumeric()" value="Submit">

当我按下这个按钮时究竟会发生什么?

var prod_id=document.getElementByID("productid").value;

  var prod_type=document.myform.producttype.value;

两种机制都一样吗?

This is the Modified one.But still not working. Please help

<!DOCTYPE html>
<html>
<head>
<script>
function validateAlphaNumeric()
{

var alphabets="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var numbers="0123456789";
var flag=true;
var prod_id=document.getElementByID("productid").value;

for(var i=0;i<prod_id.length;i++)
{
if(alphabets.indexOf(prod_id.charAt(i))==-1 && numbers.indexOf(prod_id.charAt(i)==-1))
{
alert("value must be alphanumeric");
return false;
}
}


var prod_type=document.myform.producttype.value;
for(var i=0;i<prod_type.length;i++)
{
if(alphabets.indexOf(prod_type.charAt(i))==-1 && numbers.indexOf(prod_type.charAt(i)==-1))
{
alert("value must be alphanumeric");
return false;
}
}
}
</script>
</head>
<body>

<form name="myform" action="http://localhost:8080/examples/Submit.HTML" method="POST">

<br><br>
Prodcut ID:<input type="text" name="productid" id="productid" size="25" ">
<br><br>
Product Type:<input type="text" name="producttype" id="producttype" size="25" ">
<br><br>
</div>

<input type="submit"  onclick="validateAlphaNumeric(); " value="Submit">
</form>
</body>
</html> 
4

3 回答 3

1

你去那里了,你忘记了 if 语句的右括号:

<!DOCTYPE html>
<html>
<head>
<script>
function validateAlphaNumeric()
{
alert("function start");
var alphabets="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var numbers="0123456789";
var flag=true;
var prod_id=document.getElementByID("productid").value;
alert(prod_id);
for(var i=0;i<prod_id.length;i++)
{
if(alphabets.indexOf(prod_id.charAt(i))==-1 && numbers.indexOf(prod_id.charAt(i)==-1))
{
alert("value must be alphanumeric");
break;
}
}


var prod_type=document.myform.producttype.value;
for(var i=0;i<prod_type.length;i++)
{
if(alphabets.indexOf(prod_type.charAt(i))==-1 && numbers.indexOf(prod_type.charAt(i)==-1))
{
alert("value must be alphanumeric");
break;
}
}
}
</script>
</head>
<body>

<form name="myform" action="" method="POST">

<br><br>
Prodcut ID:<input type="text" name="productid" id="productid" size="25" ">
<br><br>
Product Type:<input type="text" name="producttype" id="producttype" size="25" ">
<br><br>
</div>

<input type="submit"  onclick="validateAlphaNumeric()" value="Submit">
</form>
</body>
</html> 
于 2013-02-05T21:32:08.923 回答
0

如果你想没有去操作页面你必须把return false; 像这样在onclick结束时

onclick="validateAlphaNumeric();return false;"

或者把return false; 在您调用的函数结束时返回 false;你说浏览器不提交

于 2013-02-05T21:28:59.343 回答
0

验证失败时尝试返回 false

于 2013-02-05T21:29:10.717 回答