π κ²°κ³Ό


π‘ μμ
<head>
<style type="text/css">
/** νλμ μ
λ ₯ μμμ μ μνλ <div>νκ·Έ */
.input_group {
height: 42px;
border-bottom: 1px dotted #ccc;
font: 1em/40px 'λμ', 'Helvetica';
}
/** μ
λ ₯μμμ μ λͺ©μ λ³Ό μ μκ² νλ νκ·Έ */
.input_group > label {
width: 80px; display: inline-block;
}
</style>
</head>
<body>
<form name="form1" onsubmit="doSubmit(); return false;">
<fieldset>
<legend>νμκ°μ
</legend>
<div class="input_group first">
<label>μ΄λ¦</label>
<input type="text" name="user_name" />
</div>
<div class="input_group">
<label>μ±λ³</label>
<label><input type="radio" name="gender" value="M"> λ¨μ</label>
<label><input type="radio" name="gender" value="F"> μ¬μ</label>
</div>
<div class="input_group">
<label>μ§μ
</label>
<select name="job">
<option>----- μ ννμΈμ -----</option>
<option value="dev">νλ‘κ·Έλλ¨Έ</option>
<option value="pub">νΌλΈλ¦¬μ
</option>
</select>
</div>
<div class="input_group">
<label>μ·¨λ―Έ</label>
<label><input type="checkbox" value="μΆκ΅¬" name="hobby"> μΆκ΅¬</label>
<label><input type="checkbox" value="λꡬ" name="hobby"> λꡬ</label>
<label><input type="checkbox" value="μΌκ΅¬" name="hobby"> μΌκ΅¬</label>
</div>
<div class="input_group">
<label> </label>
<input type="submit" name="button" id="button" value="μ μΆ" />
<input type="button" name="button2" value="리μ
" />
</div>
</fieldset>
</form>
<script>
function doSubmit(){
// λ°λ³΅μμ = λ³μλ‘ λ°κΈ°
let frm = document.form1;
//validation check
//text μμμ μ
λ ₯μ¬λΆ κ²μ¬
if(!frm.user_name.value){
// κ°μ΄ μμ λ
alert('μ΄λ¦μ μ
λ ₯ν΄μ£ΌμΈμ')
document.form1.user_name.focus();
// μ¬κΈ°κΉμ§ νμΌλ©΄ μ΄ λ°μΌλ‘λ μ§ννμ§λ§
return false;
}
// λΌλμ€ λ²νΌ μ
λ ₯ μ¬λΆ κ²μ¬
if(!frm.gender[0].checked && !frm.gender[1].checked){
alert('μ±λ³μ μ νν΄μ£ΌμΈμ')
frm.gender[0].focus();
return false;
}
// select μμμ λν μ ν μμΉ κ²μ¬
if(frm.job.selectedIndex < 1){
alert('μ§μ
μ μ νν΄μ£ΌμΈμ')
frm.job.focus();
return false;
}
// 체ν¬λ°μ€ μ ν μ¬λΆ κ²μ¬
// chkκ° trueκ° λλ©΄ νλλΌλ μ ν λΌμλ€λ λ»
let chk = false;
for(let i = 0 ; i < frm.hobby.length ; i++){
if (frm.hobby[i].checked){
chk = true;
break;
}
}
if(!chk){
alert("μ·¨λ―Έλ₯Ό μ νν΄ μ£ΌμΈμ");
frm.hobby[0].focus();
return false;
}
if(confirm("μ
λ ₯νμ λ΄μ©μ΄ λ§μ΅λκΉ?")){
frm.submit();
}
}
</script>
</body>