WEB.JS/09.FORM_JS

chapter02 : input_check

GAWON 2023. 5. 22. 09:29
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
	function goLogin(){
		
		var form = document.forms.myForm;
 		
		if(form.id.value == ""){
			alert("아이디는 반드시 입력해야합니다.");
			return false;
		}
		
		if(form.pw.value == "" || 
			form.pw.value.length < 8 || 
			form.pw.value.length > 16){
			
			alert("비밀번호는 반드시 8~16자 사이로 입력해야 합니다.");
			form.pw.value = "";	// 데이터 초기화
			form.pw.focus();
			
			return false;
		}

		return true;
	}
</script>
</head>
<body>
	<!-- onsubmit은 submit 버튼일 때 사용 -->
	<form action="#" name="myForm" onsubmit="return goLogin()">
		아이디 <input type="text" name="id"><br/>
		<label for="uPw">비밀번호</label>
		<input type="password" name="pw" id="uPw"><br/>
		<input type="submit" value="로그인"><!-- submit은 onclick 없이 작업 -->
	</form>
</body>
</html>