본문 바로가기
웹프로그램

간단한 입력 유무 유효성 체크

by 세이박스 2008. 11. 7.
반응형

<script>
function formCheck(formobj){

// Field name
var fieldRequired = Array("subject","content");
// Field infomation
var fieldDescription = Array("제목","내용");
// alertMsg
var alertMsg = "다음 항목들을 입력해주세요.\n";

var l_Msg = alertMsg.length;

for (var i = 0; i < fieldRequired.length; i++){
var obj = formobj.elements[fieldRequired[i]];
if (obj){
switch(obj.type){
case "select-one":
if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
case "select-multiple":
if (obj.selectedIndex == -1){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
case "text":
case "textarea": case "password":

if (obj.value == "" || obj.value == null){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
default:
}
if (obj.type == undefined){
var blnchecked = false;
for (var j = 0; j < obj.length; j++){
if (obj[j].checked){
blnchecked = true;
}
}
if (!blnchecked){
alertMsg += " - " + fieldDescription[i] + "\n";
}
}
}
}

if (alertMsg.length == l_Msg){

return true;

}else{
alert(alertMsg);
return false;
}
}
</script>

<form name="form" method=post onsubmit="return formCheck(this);" action="/php/board_insert.php">

반응형