사용자 지정 유효성 검사 양식을 만들고 싶습니다만 어떻게 해야할지 잘 모릅니다. 양식 제출시 input이 비어있는 경우 "warning_red"클래스를 입력에 추가하려고합니다. 또한 입력 요소의 자리 표시자를 제거하고 "필수"텍스트로 바꾸고 싶습니다. 나는 그것을 달성 할 수 있지만 "필수"를 사용하여 내 HTML 있지만 jQuery 길을 알고 싶습니다. 누군가가 도울 수 있기를 바랍니다. 여기 Jquery 사용자 지정 양식 유효성 검사
내 코드입니다 :HTML :
<div class="wrapper">
<form action="#" method="post">
<label for="username">Username</label>
<input type="text" name="username" id="username" placeholder="Name">
<br>
<label for="lastname">Last Name</label>
<input type="text" name="lastname" id="lastname" placeholder="Last Name">
<br>
<label for="email">Email</label>
<input type="email" name="email" id="email" placeholder="Email">
<br>
<label for="phone">Phone</label>
<input type="tel" name="phone" id="phone" placeholder="Phone">
<br>
<input type="submit" value="Submit" id="submit_btn">
</form>
</div>
CSS 코드 :
* {
padding:0px;
margin:0px;
box-sizing: border-box;
}
body {
font-family: sans-serif;
}
.wrapper {
width:500px;
margin:0 auto;
text-align: center;
}
label {
width:100px;
float:left;
padding:9px;
margin-top:20px;
}
input {
width:300px;
padding:10px;
margin-top:20px;
}
input[type="submit"] {
width:200px;
margin:20px 0;
}
.warning_red {
color:#ff0000;
background:#d89d9d;
}
jQuery를 코드 :
$(function(){
$("#submit_btn").on("click", function(){
var texta = $(".wrapper").find("input");
if (texta.val()==="") {
texta.addClass("warning_red");
}
});
});
내가 테스트를 원해야하지만, 그 작동하지, 그리고 # 래퍼를 .wrapper로 변경했습니다. – NoName84
이 스크립트를 추가하십시오.이 스크립트를 추가하십시오. \t \t \t \t \t \t – A8ina
코드와 작업에 약간의 변경을가했지만 양식을 제출하는 것을 방해하지는 않습니다. 그냥 클래스를 추가하면 폼이 실행됩니다. – NoName84