0

如何对两个函数进行验证。

因为我可以在输入表单的正确信息(名称、主题和编号)但未选中单选按钮后转到“success.html”。

我知道它与返回函数有关。

只是想知道是否有可能拥有一个验证另一个功能的功能?

<head>
<title>Exam Entry</title>
<script language="javascript" type="text/javascript">

function validateForm() {
var result = true;
var msg="";

if (document.ExamEntry.name.value=="") {
msg+="You must enter your name \n";
document.ExamEntry.name.focus();
document.getElementById('name').style.color="red";
result = false;
}

if (document.ExamEntry.subject.value=="") {
msg+="You must enter your subject \n";
document.ExamEntry.subject.focus();
document.getElementById('subject').style.color="red";
result = false;
}

if (document.ExamEntry.examnumber.value=="") {
msg+="You must enter the examnumber \n";
document.ExamEntry.examnumber.focus();
document.getElementById('examnumber').style.color="red";
result = false;
}

if (document.ExamEntry.examnumber.value.length!=4) {
msg+="You must enter at least Four Numbers in the ExamNumber \n";
document.ExamEntry.examnumber.focus();
document.getElementById('examnumber').style.color="red";
result = false;
} 


var numbers = /^[0-9]{4}$/;


if (!(document.ExamEntry.examnumber.value.match(numbers))) {
    msg += "Only use numeric characters for the Examnumber \n";
    document.ExamEntry.examnumber.focus();
    document.getElementById('examnumber').style.color = "red";
    result = false;
}  


if(msg==""){
return result;
}
{
alert(msg)
return result;
}
}


function confirmation() {

var checked = null;
var inputs = document.getElementsByName('examtype');
for (var i = 0; i < inputs.length; i++) {
          if (inputs[i].checked) {
           checked = inputs[i];
   }
}
if(checked==null)
{
    alert('Please choose an exam level.');
    return false;
}
else{
     confirm('You have chosen '+checked.value+' is this correct?');
}

}




</script>
</head>


<body>
<h1>Exam Entry Form</h1>
<form name="ExamEntry" method="post" action="success.html">
<table width="50%" border="0">
<tr>
<td id="name">Name</td>
<td><input type="text" name="name" /></td>
<tr>
<td id="subject">Subject</td>
<td><input type="text" name="subject" /></td>
</tr>
<tr>
<td id="examnumber">Examination Number</td>
<td><input type="text" name="examnumber" maxlength="4" /></td>
</tr>
<tr>
<td><input type="radio" id="examtype" name="examtype" value="GCSE" /> : GCSE<br />
<td><input type="radio" id="examtype" name="examtype" value="A2" /> : A2<br />
<td><input type="radio" id="examtype" name="examtype" value="AS"/> : AS<br />
</tr>
<tr>
<td><input type="submit" name="Submit" value="Submit" onclick="confirmation();return validateForm

();"    />  </td>
<td><input type="reset" name="Reset" value="Reset" /></td>
</tr>
</table>
</form>
</body>
4

2 回答 2

0

最重要的是,将您的验证功能添加到您的表单中。

<form
    name="myForm"
    method="post"
    action="foo.html"
    onsubmit="return validateForm()">
    ...
</form>

除此之外,只需在另一个函数中调用该函数,然后处理返回值。

function validateForm() {

    result = true;

    //do some validation
    if(fails){
        //do some handling

        result = 0;
    }

    result = result && validateSpecial();

    return result;
}

function validateSpecial() {
    //check condition
    if(fails){
        //do stuff to handle failure

        result = 0;
    }
    return result;
}
于 2013-10-05T21:46:39.087 回答
0

基本上我把所有东西都移到一个函数中,并搞砸了'return'

现在都完成了!改进的代码为我测试并进行比较

<head>
<title>Exam Entry</title>
<script language="javascript" type="text/javascript">

function validateForm() {



var checked = null;
var inputs = document.getElementsByName('examtype');
for (var i = 0; i < inputs.length; i++) {
          if (inputs[i].checked) {
           checked = inputs[i];
   }
}
if(checked==null)
{
    alert('Please choose an exam level.');
    return false;
}
else{
      confirm('You have chosen '+checked.value+' is this correct?');
}




var result = true;
var msg="";

if (document.ExamEntry.name.value=="") {
msg+="You must enter your name \n";
document.ExamEntry.name.focus();
document.getElementById('name').style.color="red";
result = false;
}

if (document.ExamEntry.subject.value=="") {
msg+="You must enter your subject \n";
document.ExamEntry.subject.focus();
document.getElementById('subject').style.color="red";
result = false;
}

if (document.ExamEntry.examnumber.value=="") {
msg+="You must enter the examnumber \n";
document.ExamEntry.examnumber.focus();
document.getElementById('examnumber').style.color="red";
result = false;
}

if (document.ExamEntry.examnumber.value.length!=4) {
msg+="You must enter at least Four Numbers in the ExamNumber \n";
document.ExamEntry.examnumber.focus();
document.getElementById('examnumber').style.color="red";
result = false;
} 


var numbers = /^[0-9]{4}$/;


if (!(document.ExamEntry.examnumber.value.match(numbers))) {
    msg += "Only use numeric characters for the Examnumber \n";
    document.ExamEntry.examnumber.focus();
    document.getElementById('examnumber').style.color = "red";
    result = false;
}  




if(msg==""){
return result;
}
{
alert(msg)
return result;
}
}




</script>
</head>


<body>
<h1>Exam Entry Form</h1>
<form name="ExamEntry" method="post" action="success.html">
<table width="50%" border="0">
<tr>
<td id="name">Name</td>
<td><input type="text" name="name" /></td>
<tr>
<td id="subject">Subject</td>
<td><input type="text" name="subject" /></td>
</tr>
<tr>
<td id="examnumber">Examination Number</td>
<td><input type="text" name="examnumber" maxlength="4" /></td>
</tr>
<tr>
<td><input type="radio" id="examtype" name="examtype" value="GCSE" /> : GCSE<br />
<td><input type="radio" id="examtype" name="examtype" value="A2" /> : A2<br />
<td><input type="radio" id="examtype" name="examtype" value="AS"/> : AS<br />
</tr>
<tr>
<td><input type="submit" name="Submit" value="Submit" onclick="return validateForm();"/>  </td>
<td><input type="reset" name="Reset" value="Reset" /></td>
</tr>
</table>
</form>
</body>
于 2013-10-06T16:05:59.327 回答