2014-05-16 2 views
0

현재 양식 입력란에 html을 사용하고 있습니다. 또한 폼의 입력을 확인하기 위해 JavaScript 유효성 검사를 사용하고 있습니다. 현재 나는 '이름', '제목'및 '시험 번호'를 가지고 있으며, 각각 작동하고 기능 검증이 있습니다. '자격'필드의 경우 사용자가 클릭하여 자격을 선택해야하는 라디오 버튼이 있습니다. 유효성 검사가 완료되었지만 불행하게도 필드가 빨간색으로 강조 표시되고 양식이 제출되고 '자격'이 비어있을 때 오류 메시지가 표시됩니다. 자격 분야와는 별도로 모든 것이 작동합니다. 필드가 emty로 남아있을 때 텍스트가 강조 표시되고 오류 메시지가 포함되도록 누군가가 '검증'검증을 수정할 수 있다면 좋을 것입니다. 여기 자바 스크립트 라디오 버튼 html 웹 양식

감사

내 코드입니다 :

<html> 
<head> 

<title>Exam Entry</title> 

<script language="javascript" type="text/javascript"> 
function qualinform(qualname) { 
alert(qualname + " was selected as your qualification."); 
} 

function validateForm(e) { 

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 the subject \n"; 
document.ExamEntry.subject.focus(); 
document.getElementById('subject').style.color="red"; 
result = false; 
} 

if (document.ExamEntry.group1.value=="") { 
msg+="You must choose a qualification\n"; 
document.ExamEntry.group1.focus(); 
document.getElementById('radioqual').style.color="red"; 
result = false; 
} 

var regex = /^\d{4}$/; 
if (document.ExamEntry.Examination_number.value == "") { 
msg+="You must enter your examination number"; 
document.getElementById('Examination_number').style.color="red"; 
result = false; 
} else if (isNaN(document.ExamEntry.Examination_number.value)) { 
msg+="Examination number should only contain digits"; 
document.getElementById('Examination_number').style.color="red"; 
result = false; 
} else if (!regex.test(document.ExamEntry.Examination_number.value)) { 
msg+="Examination number should contain exactly 4 digits"; 
document.getElementById('Examination_number').style.color="red"; 
result = false; 
} 

if (msg != "") { 
alert(msg); 
} 

return result; 
} 
</script> 



</head> 

<body> 
<h1>Exam Entry Form</h1> 
<form name="ExamEntry" method="post" action="success.html" onsubmit="return  validateForm();"> 
<table width=”50%” border=”0”> 
<tr> 
    <td id="name">Name</td> 
    <td><input type="text" name="name" /></td> 
</tr> 
<tr> 
    <td id="subject">Subject</td> 
    <td><input type="text" name="subject" /></td> 
</tr> 
    <td id="Examination_number">Examination number</td> 
    <td><input type="text" maxlength="4" name="Examination_number" /></td> 
</tr> 
<tr> 
<td id="qualification">Choose your qualification</td> 
<tr> 
<td id="radioqual"> 
<input onclick="qualinform('GCSE');" type="radio" name="group1" value="GCSE">GCSE<br> 
<input onclick="qualinform('AS');" type="radio" name="group1" value="AS">AS<br> 
<input onclick="qualinform('A2');" type="radio" name="group1" value="A2">A2<br> 
</td> 
</tr> 
<tr> 
    <td><input type="submit" name="Submit" value="Submit" /></td> 
    <td><input type="reset" name="Reset" value="Reset" /></td> 
</tr> 
</table> 
</form> 
</body> 

</html>  

답변

1

어느 것도 선택하지 않은 그룹 1 라디오 버튼의 반환 값을 사용하면 if 문에 조건을 변경해야하지 ""예상대로, 그래서 정의되지

if (document.ExamEntry.group1.value==undefined) 
관련 문제