我正在做一個表單,我有一堆函數在窗體中來驗證某些字段,但我遇到的問題是,當您提交每個驗證返回false一個警報將彈出所以它最終成爲了很多警報,我只是想知道如何改變我自己調用函數或函數的方式,以便一次只能彈出一個警報。一次一個提醒
我的表格目前的工作原理類似於如果您提交表格並將名字和姓氏字段留空並且這兩個字段都是強制性的,則會彈出兩個警報,說明您沒有填寫姓氏並且您還沒有填寫最後一個名字(這可能會讓我煩惱的字段數量)。
我試圖找出一種方法,以便如果您提交表單並留下兩個字段空只有一個警報會彈出說你沒有填寫名字,然後當你填寫第一個名稱字段並且仍然將姓氏字段留空,那麼將會彈出一個提示,說您沒有填寫姓氏。
下面是驗證功能的主要塊i具有:
function validateText()
{
var firstname=document.getElementById('txtfirstname');
var familyname=document.getElementById('txtfamilyname');
var streetaddress=document.getElementById('txtaddress');
var suburb=document.getElementById('txtsuburb');
var postcode=document.getElementById('txtpostcode');
var country=document.getElementById('txtcountry');
var telephone=document.getElementById('txttelephone');
var email=document.getElementById('txtemail');
var regodate=document.getElementById('txtregodate');
var regocost=document.getElementById('txtregocost');
if (firstname.value=="")
{
alert("First name must be filled out");
return false;
}
if (familyname.value=="")
{
alert("Family name must be filled out");
return false;
}
var institutioncompany=document.getElementById('txtinstcomp').value;
if (institutioncompany == "")
{
alert("Institution/company must be filled out");
return false;
}
var category=document.getElementById('category').value;
if (category == "UWS Student" || category == "Other UWS Staff" || category == "UWS Academic")
{
if (document.getElementById("txtnumber").value == "")
{
alert('Student number/Staff number is mandatory');
return;
}
}
if (streetaddress.value=="")
{
alert("Street address must be filled out");
return false;
}
if (suburb.value=="")
{
alert("Suburb must be filled out");
return false;
}
if (postcode.value=="")
{
alert("Postcode must be filled out");
return false;
}
if (country.value=="")
{
alert("Country must be filled out");
return false;
}
if (telephone.value=="")
{
alert("Telephone number must be filled out");
return false;
}
if (email.value=="")
{
alert("Email address must be filled out");
return false;
}
if (regodate.value=="")
{
alert("Date of registration must be filled out");
return false;
}
if (regocost.value=="")
{
alert("Cost of registration must be filled out");
return false;
}
}
function validateCheckBoxes(theForm)
{
if (
theForm.checkbox.checked == false &&
theForm.checkbox1.checked == false &&
theForm.checkbox2.checked == false)
{
alert ('You didn\'t choose any of the checkboxes');
return false;
} else {
return true;
}
}
function validateRadioButton()
{
var radios = document.getElementsByName('yesno');
for (var i = 0; i < radios.length; i++)
{
if (radios[i].checked)
{
return true; //checked
}
};
// not checked, show error
alert ('You didn\'t choose whether you wanted a copy of work proceedings');
return false;
}
function validateEmail()
{
var emailID = document.rego.email.value;
atpos = emailID.indexOf("@");
dotpos = emailID.lastIndexOf(".");
if (atpos < 1 || (dotpos - atpos < 2))
{
alert("Please enter correct email ID");
return false;
}
return(true);
}
這裏爲i調用一些功能的方式:
<form name="rego" action="submit.htm" onsubmit="return !!(validateText() & validateCheckBoxes(this) & validateRadioButton() & validateEmail() & populateInstitution());" method="POST">
你不返回false無處不在,你的警報,你想&&,不與 – mplungjan
哦,是這麼簡單, 非常感謝! – bigsenator