본문으로 바로가기


폼 유효성 검사


<form id="fmField" name="fmField" action="" method="post" onsubmit="return checkForm();">
<div class="form">
<table class="ftb">
<tr>
<td>아이디</td>
<td><input type="text" name="userId" id="userId" /></td>
</tr>
<tr>
<td>암호</td>
<td><input type="text" name="userPw" id="userPw" /></td>
</tr>
<tr>
<td colspan="2">
<!-- submint 일경우는 onsubmit="return checkForm();"-->
<input type="submit" value="로그인" />
<!-- button 일 경우는 아래 코드처럼 -->
<!--<input type="button" onclick="checkForm"></button>-->
<input type="reset" value="취소">
</td>
</tr>
</table>
</div>
</form>



function checkForm() {
var userId = document.fmField.uerId;
// 아이디 입력 유무 체크
if(userId.value == '' || !(userId.value.length >= 3 && userId.value.length <= 12)) {
window.alert("아이디를 입력하시오");
document.fmField.uerId.focus();
document.getElementById('userId').select();
return false; // 아이디 입력이 안되어 있다면 submint 이벤트를 중지
}
var uerPw = document.getElementById('userPw');
// 암호 입력 유무 체크
if(document.fmField.userPw.value == ''){
alert('암호를 입력하세요.');
userPw.focus();
return false;
}
}