userAgree == false means !$("#userAgreement").is(":checked")
Thymeleaf control check box in javascript
-
26-06-2023 - |
Question
I have a form which has a check box.After submitting the form i want to control if user checked the box.
Here is my code sample:
<form action="DoLogin" method="post" id="loginForm">
<h3>Giriş</h3>
<label><span>Kullanıcı:</span></label> <input type="text" placeholder="E-posta adresinizi Giriniz" class="round" name="userName" id="userName" required="true" ></input>
<label><span>Parola:</span></label> <input type="text" placeholder="Parolanızı Giriniz...." name="password" id="password" required="true" ></input>
<label>Giriş Yaparak <span href="#" style="color: red;">Kullanıcı Sözleşmesini</span> Kabul Ediyorum...</label> <input id="userAgreement" type="checkbox" th:checked="${true}"></input>
<label for="checkbox1" class="round" th:checked="${true}" id="userAgreement" >Beni Hatırla</label>
<a href="#" class="button login round" onclick="login();return false">Giriş</a>
</form>
Here is my js code:
function login(){
var control=true;
userName = $('#userName').val(),
password = $('#password').val(),
userAgree = $('#userAgreement').checked`enter code here`
if(userName.length == 0 ) {
alert('Lütfen e-posta adresini giriniz.')
return false;
}else if( password.length == 0 ){
alert('Lütfen parolanızı giriniz.')
return false;
}else if( userAgree == false ){
alert('Lütfen kullanıcı sözleşmesini okudum bölümünü işaretleyiniz.')
return false;
}
$('#loginForm').submit();
};
Solution
OTHER TIPS
//override the form submit function that will return false when check box is not checked and
// will not submit the form
$("form").submit(function(){
if(!$("#userAgreement").is(":checked"))
{
alert("Please check the agreement");
return false;
}
});
// add above code in your js
function login(){
var control=true;
userName = $('#userName').val(),
password = $('#password').val(),
userAgree = $('#userAgreement').checked`enter code here`
if(userName.length == 0 ) {
alert('Lütfen e-posta adresini giriniz.')
return false;
}else if( password.length == 0 ){
alert('Lütfen parolanızı giriniz.')
return false;
}else if( userAgree == false ){
alert('Lütfen kullanıcı sözleşmesini okudum bölümünü işaretleyiniz.')
return false;
}
$('#loginForm').submit();
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow