[英]Check box and text field validation javascript
我在移動網站上有一個文本字段和一個復選框,在提交表單之前需要它。 一個是 zip 代碼搜索,需要 go 上,還有一個條款和條件的復選框。
現在我有文本字段驗證工作......
<script>
function validateForm() {
var x=document.forms["searchform"]["fromAddress"].value;
if (x==null || x=="") {
alert("Oops! You forgot to enter a location.");
return false;
}
}
</script>
但我不知道如何在復選框中添加...下面是表單的代碼:
<form name="searchform" method="post" action="list.php" onsubmit="return validateForm()">
<input type="hidden" name="search" value="1" />
<input class="txtfield" type="search" id="search" name="fromAddress" onfocus="this.value=''" />
<div class="terms-container">
<div class="terms-checkbox-container">
<input class="acceptterms" type="checkbox" name="agree" value="agree_terms">
</div>
<div class="terms-text-container">
I Agree to the Terms and Conditions
</div>
</div>
<input class="btn-submit" type="submit" id="submit" value="Go!"/>
</form>
任何幫助都會很棒。 謝謝!
您可以使用checked
屬性獲取復選框的狀態。 因此,您可以執行以下操作。
<script>
function validateForm() {
var x=document.forms["searchform"]["fromAddress"].value;
var y=document.forms["searchform"]["agree"].checked;
if (x==null || x=="") {
alert("Oops! You forgot to enter a location.");
return false;
}
if (y === false) {
alert("Oops! You forgot to agree to the terms and Conditions");
return false;
}
}
</script>
使用以下代碼:
var agreeCheckbox = document.forms["searchform"]["agree"];
if(!agreeCheckbox.checked) {
alert('You need to accept terms to submit');
return false;
}
return true;
你可以用
function validateForm()
{
var x=document.forms["searchform"]["fromAddress"].value;
if (x==null || x=="")
{
alert("Oops! You forgot to enter a location.");
return false;
}
var checkbox = document.forms['searchform']['agree'];
if (!checkbox.checked) {
alert('Oops! You must agree with Terms');
return false;
}
return true;
}
我相信您可以檢查復選框的checked
屬性:
var checked = document.forms["searchform"]["agree"].checked;
if (!checked) {
alert("Oops! Please check the checkbox!");
return false;
}
您可以在復選框中使用此答案進行表單驗證
//checkbox var m=document.getElementById("client"); console.log(m) if(.m.checked){ document.getElementById("bekar");innerHTML="please agree term & condition ". document.getElementById("bekar"):style="color;red"; return false. } else{ document.getElementById("bekar");innerHTML=""; }
<input type="checkbox" class="form-check-input" id="isclient" required> <label class="form-check-label" for="isclient"> I want you to work on a project with me</label><br><span id="bekar"></span>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.