채택완료

input 입력 여부 체크 입니다.

Copy
<input type="test" class="_check_inp_" title="이름"><br>
<input type="test" class="_check_inp_" title="전화번호"><br>
<input type="test" class="_check_inp_" title="이메일"><br>
<input type="test" class="_check_inp_" title="주소"><br>

 

<button type="button" class="btn">확인</button>

 

같은 클래스로  input이 있을때 

입력값이 없으면

(input title)을 입력하세요. 

라고 alert를 띄우고 싶습니다.

 

반복문을 통해 간단하게(?) 작성할 수 있을까요?

 

|

답변 1개

채택된 답변
+20 포인트

Copy
let _check=true;
$("._check_inp_").each(function(){
    if(!$.trim($(this).val()))
    {
        alert($(this).attr('title')+" 을 입력하세요.");
        $(this).focus();
        _check=false;
        return false;
    }
});
if(!_check) return false;

폼 전송시 유효성 검사하는 부분에 위 구문 추가하시면 될듯합니다. 

답변을 작성하려면 로그인이 필요합니다.