为什么js验证没通过,还会submit呢?该怎么做才行呢

<script language="javascript">
function isNumber(String)
{
var Letters = "1234567890";
var i;
var c;
for( i = 0; i < String.length; i ++ )
{
c = String.charAt( i );
if (Letters.indexOf( c ) < 0)
return false;
}
return true;
}
function CheckForm()
{
if (!isNumber(document.myform.scale.value)) {
alert("You must input a num for the scale!");
document.myform.scale.focus();
return false;
}
if (!isNumber(document.myform.replication.value)) {
alert("You must input a num for the replication!");
document.myform.replication.focus();
return false;
}
if (document.myform.scale.value.length == 0) {
alert("Please input the scale of the cluster!");
document.form.myform.focus();
return false;
}
if (document.myform.replication.value.length == 0) {
alert("Please input the replication of the cluster!");
document.form.myform.focus();
return false;
}
return true;
}
</script>
<form action="/horizon/project/" method="post" name="myform" onsubmit="return CheckForm()">

<label>Scale</label>
<input type="text" name="scale" />

<br />

<label>Replication</label>
<input type="text" name="replication" />

<br />

<input type="submit" value="Launch" />

</form>

<form action="" method="post" id="myform" name="form1">
<input type="button" onclick="checkForm()"/><!--注意这里尽量不要使用submit,容易被js注入,并且有的浏览器可忽略js加载-->

funtion checkForm(){
//这里是你做验证的代码
subForm();
}
function subForm(){
form1.action="*.action";
form1.submit();
}追问

form1.action="*.action";
form1.submit();
您这两行是什么意思啊,能不能解释下。谢谢

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-08-26
type="submit" 用button 然后 ONCLICK="CheckForm()"
第2个回答  推荐于2016-11-26
不需要这么做吧?你这样做看的人头晕。这样写多简单:
<form action="" id="myform">
………………
<input type="button" onclick="CheckForm()"/>
</form>
function isNumber(String)
{
var Letters = "1234567890";
var i;
var c;
for( i = 0; i < String.length; i ++ )
{
c = String.charAt( i );
if (Letters.indexOf( c ) < 0)
return false;
}
return true;
}
function CheckForm()
{
if (!isNumber(document.myform.scale.value)) {
alert("You must input a num for the scale!");
document.myform.scale.focus();

}
else if (!isNumber(document.myform.replication.value)) {
alert("You must input a num for the replication!");
document.myform.replication.focus();

}
else if (document.myform.scale.value.length == 0) {
alert("Please input the scale of the cluster!");
document.form.myform.focus();

}
else if (document.myform.replication.value.length == 0) {
alert("Please input the replication of the cluster!");
document.form.myform.focus();

}else{
document.getElementById("myform").submit();
}
}追问

您写的确实舒服多了。问下,和是不是没啥区别啊?

另外我上面哪个,如果返回了false,是不是不该跳转了,但是实际上还是跳转了,您再帮忙看下。谢了

本回答被提问者采纳