Hello,
I am learning JavaScipt. I have written some functions and I verified my
code using http://www.jslint.com/ validator. I don't understand the
problems it reports. Could you help me please and invoke
http://www.jslint.com/ validator?
Thanks for help.
Here's the code:
var reEmail = /^[a-z0-9-.]{1,40}@[a-z0-9-.]{1,70}$/;
var reDate = /^(\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/;
var error_message = "";
// sprawdzać długości pól
function ValidateClient( )
{
var ok = true;
document.announ cement.client_e mail.className = "";
document.announ cement.client_m obile.className = "";
document.announ cement.client_p hone.className = "";
document.announ cement.client_t own.className = "";
document.announ cement.client_d irection.classN ame = "";
if (document.annou ncement.client_ email.value !== ""
&& !reEmail.test(d ocument.announc ement.client_em ail.value))
{
ok = false;
error_message += "Twój e-mail jest niepoprawny.\n" ;
document.announ cement.client_e mail.className = "error_fiel d";
}
if (document.annou ncement.client_ email.value === "" &&
document.announ cement.client_p hone.value === "" &&
document.announ cement.client_m obile.value === "")
{
ok = false;
error_message += "Proszę wprowadzić albo swój e-mail albo swój telefon
\n";
document.announ cement.client_e mail.className = "error_fiel d";
document.announ cement.client_m obile.className = "error_fiel d";
document.announ cement.client_p hone.className = "error_fiel d";
}
if (document.annou ncement.client_ town.value === "" &&
document.announ cement.client_d irection.value === "" &&
document.announ cement.client_p hone.value !== "")
{
ok = false;
error_message += "Proszę wprowadzić albo swoją miejscowość albo swój numer
kierunkowy.\n";
document.announ cement.client_t own.className = "error_fiel d";
document.announ cement.client_d irection.classN ame = "error_fiel d";
}
return ok;
}
function ClearClient()
{
document.announ cement.client_e mail.value = "";
document.announ cement.client_m obile.value = "";
document.announ cement.client_d irection.value = "";
document.announ cement.client_p hone.value = "";
document.announ cement.client_t own.value = "";
}
function ValidatePropert y()
{
var ok = true;
document.announ cement.floor_no .className = "";
document.announ cement.rooms.cl assName = "";
document.announ cement.year_bui lt.className = "";
if (document.annou ncement.floor_n o.value !== "" &&
isNaN(document. announcement.fl oor_no.value))
{
ok = false;
error_message += "Niepoprawn y numer piętra.\n";
document.announ cement.floor_no .className = "error_fiel d";
}
if (document.annou ncement.rooms.v alue !== "" &&
(isNaN(document .announcement.r ooms) || document.announ cement.rooms.va lue <
0))
{
ok = false;
error_message += "Niepoprawn a liczba pokoi.\n";
document.announ cement.rooms.cl assName = "error_fiel d";
}
var today = Date();
if (document.annou ncement.year_bu ilt.value !== "" &&
(isNaN(document .announcement.y ear_built.value ) ||
document.announ cement.year_bui lt.value today.getFullYe ar()))
{
ok = false;
error_message += "Niepoprawn y rok budowy.\n";
document.announ cement.year_bui lt.className = "error_fiel d";
}
// długość pola Uwagi
return ok;
}
function ClearProperty()
{
document.announ cement.floor_no .value = "";
document.announ cement.rooms.va lue = "";
document.announ cement.year_bui lt.value = "";
}
function isDate(strDate)
{
var matchArray = strDate.match(r eDate);
if (matchArray === null)
{
return false;
}
month = matchArray[1];
day = matchArray[3];
year = matchArray[5];
if (month < 1 || month 12)
{
return false;
}
if (day < 1 || day 31)
{
return false;
}
if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31)
{
return false;
}
if (month === 2)
{
var isLeap = (year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0));
if (day 29 || (day == 29 && !isLeap))
{
return false;
}
}
return true;
}
function ValidateAnnounc ement()
{
var ok = true;
document.announ cement.price.cl assName = "";
document.announ cement.valid_fr om.className = "";
document.announ cement.valid_to .className = "";
document.announ cement.offer_ty pe.className = "";
document.announ cement.price_fo r.className = "";
if (document.annou ncement.price.v alue !== "" &&
(isNaN(document .announcement.p rice.value) ||
document.announ cement.price.va lue < 0))
{
ok = false;
error_message += "Niepoprawn a cena dla oferty.\n";
document.announ cement.price.cl assName = "error_fiel d";
}
if (document.annou ncement.valid_f rom.value !== ""
&& !isDate(documen t.announcement. valid_from.valu e))
{
ok = false;
error_message += "Niepoprawn a data od kiedy ważne.\n";
document.announ cement.valid_fr om.className = "error_fiel d";
}
if (document.annou ncement.valid_t o.value !== ""
&& !isDate(documen t.announcement. valid_to.value) )
{
ok = false;
error_message += "Niepoprawn a data do kiedy ważne.\n";
document.announ cement.valid_to .className = "error_fiel d";
}
if (document.annou ncement.valid_f rom.value !== "" &&
document.announ cement.valid_to .value !== "" &&
document.announ cement.valid_fr om.value >
document.announ cement.valid_to .value)
{
ok = false;
error_message += "Data od kiedy ważne nie może być późniejsza niż data do
kiedy ważne.\n";
document.announ cement.valid_fr om.className = "error_fiel d";
document.announ cement.valid_to .className = "error_fiel d";
}
if (document.annou ncement.offer_t ype.value === "")
{
ok = false;
error_message += "Proszę wybrać rodzaj oferty.\n";
document.announ cement.offer_ty pe.className = "error_fiel d";
}
if (document.annou ncement.price.v alue !== "")
{
switch (document.annou ncement.offer_t ype.value)
{
case "W":
case "N":
if (document.annou ncement.price_f or.value != "M" &&
document.announ cement.price_fo r.value != "OM" &&
document.announ cement.price_fo r.value != "MOL" &&
document.announ cement.price_fo r.value != "MCZ" &&
document.announ cement.price_fo r.value != "MCZOL" &&
document.announ cement.price_fo r.value != "MZCZOL" &&
document.announ cement.price_fo r.value != "OMOL")
{
ok = false;
error_message += "Niepoprawn ie określono za co cena.\n";
document.announ cement.price_fo r.className = "error_fiel d";
}
break;
case "K":
case "S":
if (document.annou ncement.price_f or.value != "N")
{
ok = false;
error_message += "Niepoprawn ie określono za co cena, powinna
być 'nieruchomość '.\n";
document.announ cement.price_fo r.className = "error_fiel d";
}
break;
case "Z":
if (document.annou ncement.price_f or.value !== "")
{
ok = false;
error_message += "Niepoprawn ie określono za co cena, powinno być 'nie
dotyczy'.\n";
document.announ cement.price_fo r.className = "error_fiel d";
}
break;
}
}
if (document.annou ncement.caution )
{
if (document.annou ncement.offer_t ype.value != "W" &&
document.announ cement.offer_ty pe.value != "N")
{
ok = false;
error_message += "Kaucja ma sens tylko w przypadku najmu/wynajmu.\n";
document.announ cement.caution. className = "error_fiel d";
}
}
// długość pola Uwagi
return ok;
}
function ClearAnnounceme nt()
{
document.announ cement.price.va lue = "";
document.announ cement.valid_fr om.value = "";
document.announ cement.valid_to .value = "";
document.announ cement.offer_ty pe.value = "";
document.announ cement.price_fo r.value = "";
}
function AgencyRegionsSe lected()
{
for (var i = 0; i < document.announ cement.agency_r egion.length; i++)
{
if (document.annou ncement.agency_ region[i].checked === true)
{
return true;
}
}
return false;
}
function ValidateAgencie s()
{
var ok = false;
document.announ cement.agency_w holecountry.cla ssName = "";
if (!document.anno uncement.agency _wholecountry.c hecked === true
&& !AgencyRegionsS elected())
{
ok = true;
document.announ cement.agency_w holecountry.cla ssName = "error_fiel d";
error_message += "Proszę dokonać wyboru agencji.\n";
}
return ok;
}
function ValidateAndSubm it()
{
error_message = "";
if (ValidateClient () === true && ValidatePropert y() === true &&
ValidateAnnounc ement() === true && ValidateAgencie s() === true)
{
if (confirm('Czy jesteÅ› pewien?'))
{
document.announ cement.action = "announcement.p hp";
document.announ cement.submit() ;
}
}
else
{
alert(error_mes sage);
}
}
function ShowPicture(pic ture_no)
{
var w = window.open("pi cture.php?pictu re=" +
picture_no.toSt ring(), "Zdjęcie nr " +
picture_no, "location=1,sta tus=1,scrollbar s=1,width=200,h eight=200");
w.moveTo(100, 100);
}
function RemovePicture(p icture_no)
{
if (window.confirm ('Czy na pewno chcesz usunąć zdjęcie nr ' + picture_no
+ '?'))
{
document.announ cement.action = "remove_picture .php?picture=" +
picture_no.toSt ring();
document.announ cement.submit() ;
}
}
function ClearAgencyRegi ons()
{
for (var i = 0; i < 16; i++)
{
document.announ cement.agency_r egion[i].checked = false;
}
}
function ClearAgency()
{
ClearAgencyRegi ons();
document.anounc ement.agency_wh olecountry.valu e = "";
}
function PrintAnnounceme nt()
{
document.announ cement.action = "print.php" ; // pobrać dane z formularza a
nie bazy
document.announ cement.submit() ;
}
function CalculatePaymen t()
{
var agencies = 0;
if (document.annou ncement.agency_ wholecountry)
{
agencies = agencies_in_Pol and;
}
else
{
for (var i = 0; i < 16; i++)
{
if (document.annou ncement.agency_ region[i].checked === true)
{
agencies += agencies_in_reg ions[i];
}
}
}
var payment = 0;
if (agencies 0)
{
payment = fixed_cost + Math.floor(agen cies * 1.0 /
agencies_per_va riable_cost) * variable_cost;
}
document.announ cement.payment. value = payment.toFixed (2);
}