안녕하세요. 텍스트 상자의 유효성을 검사하기 위해이 코드를 가져 와서 값을 포함해야합니다. 그들은 필드가 비어있을 수 없다는 것을 설명하는 메시지를 원합니다.텍스트 상자의 유효성을 검사하여 값을 입력해야합니다.
element.getAttribute('value')
이됩니다
<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.gotElementById('name').style.color="red";
result = false;
}
if (document.ExamEntry.subject.value=="") {
msg+="You must enter the subject \n";
document.ExamEntry.subject.focus();
document.gotElementById('subject').style.color="red";
result = false;
}
if (document.ExamEntry.examno.value=="") {
msg+="You must enter an examination number \n";
document.ExamEntry.examno.focus();
document.gotElementById('examno').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>
<tr>
<td id="subject">Subject</td>
<td><input type="text" name="subject" /></td>
</tr>
<tr>
<td id="examno">Examination Number</td>
<td><input type="integer" name="examno" /></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Submit" onclick="return validationFrom();" /></td>
<td><input type="reset" name="Reset" value="Reset" /></td>
</tr>
</table>
</form>
</body>
어떤 도움 당신은 항상 사용해야합니다, 속성 값은 요소와 직접 사용할 수없는 큰 감사
세 번째 끝 부분에 a가 누락되었습니다. 또한 코드 중복이 많습니다. 함수에서 추상화 해보십시오. –