By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,287 Members | 1,665 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,287 IT Pros & Developers. It's quick & easy.

newbie: some errors reported by validator

P: n/a
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.announcement.client_email.className = "";
document.announcement.client_mobile.className = "";
document.announcement.client_phone.className = "";
document.announcement.client_town.className = "";
document.announcement.client_direction.className = "";
if (document.announcement.client_email.value !== ""
&& !reEmail.test(document.announcement.client_email.v alue))
{
ok = false;
error_message += "Twój e-mail jest niepoprawny.\n";
document.announcement.client_email.className = "error_field";
}
if (document.announcement.client_email.value === "" &&
document.announcement.client_phone.value === "" &&
document.announcement.client_mobile.value === "")
{
ok = false;
error_message += "Proszę wprowadzić albo swój e-mail albo swój telefon
\n";
document.announcement.client_email.className = "error_field";
document.announcement.client_mobile.className = "error_field";
document.announcement.client_phone.className = "error_field";
}
if (document.announcement.client_town.value === "" &&
document.announcement.client_direction.value === "" &&
document.announcement.client_phone.value !== "")
{
ok = false;
error_message += "Proszę wprowadzić albo swoją miejscowość albo swój numer
kierunkowy.\n";
document.announcement.client_town.className = "error_field";
document.announcement.client_direction.className = "error_field";
}
return ok;
}

function ClearClient()
{
document.announcement.client_email.value = "";
document.announcement.client_mobile.value = "";
document.announcement.client_direction.value = "";
document.announcement.client_phone.value = "";
document.announcement.client_town.value = "";
}

function ValidateProperty()
{
var ok = true;
document.announcement.floor_no.className = "";
document.announcement.rooms.className = "";
document.announcement.year_built.className = "";
if (document.announcement.floor_no.value !== "" &&
isNaN(document.announcement.floor_no.value))
{
ok = false;
error_message += "Niepoprawny numer piętra.\n";
document.announcement.floor_no.className = "error_field";
}
if (document.announcement.rooms.value !== "" &&
(isNaN(document.announcement.rooms) || document.announcement.rooms.value <
0))
{
ok = false;
error_message += "Niepoprawna liczba pokoi.\n";
document.announcement.rooms.className = "error_field";
}
var today = Date();
if (document.announcement.year_built.value !== "" &&
(isNaN(document.announcement.year_built.value) ||
document.announcement.year_built.value today.getFullYear()))
{
ok = false;
error_message += "Niepoprawny rok budowy.\n";
document.announcement.year_built.className = "error_field";
}
// długość pola Uwagi
return ok;
}

function ClearProperty()
{
document.announcement.floor_no.value = "";
document.announcement.rooms.value = "";
document.announcement.year_built.value = "";
}

function isDate(strDate)
{
var matchArray = strDate.match(reDate);
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 ValidateAnnouncement()
{
var ok = true;
document.announcement.price.className = "";
document.announcement.valid_from.className = "";
document.announcement.valid_to.className = "";
document.announcement.offer_type.className = "";
document.announcement.price_for.className = "";
if (document.announcement.price.value !== "" &&
(isNaN(document.announcement.price.value) ||
document.announcement.price.value < 0))
{
ok = false;
error_message += "Niepoprawna cena dla oferty.\n";
document.announcement.price.className = "error_field";
}
if (document.announcement.valid_from.value !== ""
&& !isDate(document.announcement.valid_from.value))
{
ok = false;
error_message += "Niepoprawna data od kiedy ważne.\n";
document.announcement.valid_from.className = "error_field";
}
if (document.announcement.valid_to.value !== ""
&& !isDate(document.announcement.valid_to.value))
{
ok = false;
error_message += "Niepoprawna data do kiedy ważne.\n";
document.announcement.valid_to.className = "error_field";
}
if (document.announcement.valid_from.value !== "" &&
document.announcement.valid_to.value !== "" &&
document.announcement.valid_from.value >
document.announcement.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.announcement.valid_from.className = "error_field";
document.announcement.valid_to.className = "error_field";
}
if (document.announcement.offer_type.value === "")
{
ok = false;
error_message += "Proszę wybrać rodzaj oferty.\n";
document.announcement.offer_type.className = "error_field";
}
if (document.announcement.price.value !== "")
{
switch (document.announcement.offer_type.value)
{
case "W":
case "N":
if (document.announcement.price_for.value != "M" &&
document.announcement.price_for.value != "OM" &&
document.announcement.price_for.value != "MOL" &&
document.announcement.price_for.value != "MCZ" &&
document.announcement.price_for.value != "MCZOL" &&
document.announcement.price_for.value != "MZCZOL" &&
document.announcement.price_for.value != "OMOL")
{
ok = false;
error_message += "Niepoprawnie określono za co cena.\n";
document.announcement.price_for.className = "error_field";
}
break;
case "K":
case "S":
if (document.announcement.price_for.value != "N")
{
ok = false;
error_message += "Niepoprawnie określono za co cena, powinna
być 'nieruchomość'.\n";
document.announcement.price_for.className = "error_field";
}
break;
case "Z":
if (document.announcement.price_for.value !== "")
{
ok = false;
error_message += "Niepoprawnie określono za co cena, powinno być 'nie
dotyczy'.\n";
document.announcement.price_for.className = "error_field";
}
break;
}
}
if (document.announcement.caution)
{
if (document.announcement.offer_type.value != "W" &&
document.announcement.offer_type.value != "N")
{
ok = false;
error_message += "Kaucja ma sens tylko w przypadku najmu/wynajmu.\n";
document.announcement.caution.className = "error_field";
}
}
// długość pola Uwagi
return ok;
}

function ClearAnnouncement()
{
document.announcement.price.value = "";
document.announcement.valid_from.value = "";
document.announcement.valid_to.value = "";
document.announcement.offer_type.value = "";
document.announcement.price_for.value = "";
}

function AgencyRegionsSelected()
{
for (var i = 0; i < document.announcement.agency_region.length; i++)
{
if (document.announcement.agency_region[i].checked === true)
{
return true;
}
}
return false;
}

function ValidateAgencies()
{
var ok = false;
document.announcement.agency_wholecountry.classNam e = "";
if (!document.announcement.agency_wholecountry.checke d === true
&& !AgencyRegionsSelected())
{
ok = true;
document.announcement.agency_wholecountry.classNam e = "error_field";
error_message += "Proszę dokonać wyboru agencji.\n";
}
return ok;
}

function ValidateAndSubmit()
{
error_message = "";
if (ValidateClient() === true && ValidateProperty() === true &&
ValidateAnnouncement() === true && ValidateAgencies() === true)
{
if (confirm('Czy jesteś pewien?'))
{
document.announcement.action = "announcement.php";
document.announcement.submit();
}
}
else
{
alert(error_message);
}
}

function ShowPicture(picture_no)
{
var w = window.open("picture.php?picture=" +
picture_no.toString(), "Zdjęcie nr " +
picture_no, "location=1,status=1,scrollbars=1,width=200,height =200");
w.moveTo(100, 100);
}

function RemovePicture(picture_no)
{
if (window.confirm('Czy na pewno chcesz usunąć zdjęcie nr ' + picture_no
+ '?'))
{
document.announcement.action = "remove_picture.php?picture=" +
picture_no.toString();
document.announcement.submit();
}
}

function ClearAgencyRegions()
{
for (var i = 0; i < 16; i++)
{
document.announcement.agency_region[i].checked = false;
}
}

function ClearAgency()
{
ClearAgencyRegions();
document.anouncement.agency_wholecountry.value = "";
}

function PrintAnnouncement()
{
document.announcement.action = "print.php"; // pobrać dane z formularza a
nie bazy
document.announcement.submit();
}

function CalculatePayment()
{
var agencies = 0;
if (document.announcement.agency_wholecountry)
{
agencies = agencies_in_Poland;
}
else
{
for (var i = 0; i < 16; i++)
{
if (document.announcement.agency_region[i].checked === true)
{
agencies += agencies_in_regions[i];
}
}
}
var payment = 0;
if (agencies 0)
{
payment = fixed_cost + Math.floor(agencies * 1.0 /
agencies_per_variable_cost) * variable_cost;
}
document.announcement.payment.value = payment.toFixed(2);
}
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
In article <g3**********@news2.task.gda.pl>, r_********@poczta.onet.pl wrote:
>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.
Why don't *you* invoke it, show us the problems it reports, and explain why
you don't understand them?
Jun 27 '08 #2

P: n/a
Why don't *you* invoke it, show us the problems it reports, and explain
why you don't understand them?
OK, no problem. Here's list of errors:

---
Error:

Implied global: agencies_in_Poland 301, agencies_in_regions 309,
agencies_per_variable_cost 316, alert 256, confirm 248, day 94, document 11
43 53 81 122 211 220 233 250 271 280 287 292 299, fixed_cost 316, month 93,
variable_cost 316, window 263 269, year 95

Problem at line 234 character 73: Missing 'new' prefix when invoking a
constructor.

if (!document.announcement.agency_wholecountry.checke d === true
&& !AgencyRe...

Problem at line 246 character 9: Missing 'new' prefix when invoking a
constructor.

if (ValidateClient() === true && ValidateProperty() === true &&
ValidateAnno...

Problem at line 246 character 38: Missing 'new' prefix when invoking a
constructor.

if (ValidateClient() === true && ValidateProperty() === true &&
ValidateAnno...

Problem at line 246 character 69: Missing 'new' prefix when invoking a
constructor.

if (ValidateClient() === true && ValidateProperty() === true &&
ValidateAnno...

Problem at line 246 character 104: Missing 'new' prefix when invoking a
constructor.

if (ValidateClient() === true && ValidateProperty() === true &&
ValidateAnno...

Problem at line 286 character 5: Missing 'new' prefix when invoking a
constructor.

ClearAgencyRegions();
---

I don't understand why ClearAgencyRegions() and ValidateClient() is treated
as invoking constructor. I have such functions defined, and I don't
understand why validator doesn't see a function call in the commands.
Please explain.
Thanks.
/RAM/

Jun 27 '08 #3

P: n/a
In comp.lang.javascript message <g3**********@news2.task.gda.pl>, Wed,
18 Jun 2008 21:57:04, r_********@poczta.onet.pl posted:
>var reEmail = /^[a-z0-9-.]{1,40}@[a-z0-9-.]{1,70}$/;
^^^^^^^^^^^^^^^^
That will reject valid addresses, not only because it rejects upper-
case. See <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.
>var reDate = /^(\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/;
^^^^ ^^^^
The second separator should match the first : use a backreference, \2 .

>function isDate(strDate)
{
var matchArray = strDate.match(reDate);
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;
}
That can be done very much more simply; see via
<URL:http://www.merlyn.demon.co.uk/js-dates.htm>. A detail on your
method : there is no need to calculate isLeap unless the month is 2 AND
the day is 29.
>function ClearAnnouncement()
{
document.announcement.price.value = "";
document.announcement.valid_from.value = "";
document.announcement.valid_to.value = "";
document.announcement.offer_type.value = "";
document.announcement.price_for.value = "";
}
That would be nicer as

function ClearAnnouncement()
{ var da = document.announcement
da.price.value = "";
da.valid_from.value = "";
da.valid_to.value = "";
da.offer_type.value = "";
da.price_for.value = "";
}
if (ValidateClient() === true
^^^^^^^^
There should be no need for === true or == true if
ValidateClient is properly written to return a Boolean.

The preferred right margin for News is about 72 characters; and code
posted should be executable as is. Therefore, indenting in units of two
spaces is better than indenting in tabs.

--
(c) John Stockton, nr London UK. replyYYWW merlyn demon co uk Turnpike 6.05.
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html-Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm: about usage of News.
No Encoding. Quotes precede replies. Snip well. Write clearly. Mail no News.
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.