473,387 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

newbie: some errors reported by validator

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
3 1370
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Massimo Soricetti | last post by:
Hello, I'm not a C newbie, but I'm teaching C programming (well... FIRST programming and then C) to other guys these days and it's driving me to some reflexions on the language. It's not...
4
by: Allthumbs | last post by:
Hi, Both laptops seem to have the same set up office 2002 and windows XP. But copyfromrecordset works fine on one PC and fails in another. It does not throw any exception. Help!! thanks,
0
by: John Oliver | last post by:
I'm having a devil of a time with phpBT. It displays nothing but a blank screen. I'm trying to get some kind of indication of what's going on, but am having no luck whatsoever. I've tried: ...
13
by: kolmogolov | last post by:
/* Hi, I have removed things irrelevant to reproducing the problem. What's wrong with my code? Thanks in advance for any hint! 1166425362 */ #include <stdio.h> #include <stdlib.h> #include...
6
by: quanghoc | last post by:
Hi all, I used XMLSpy to validate my XML but it catched one error at a time. Is there anyway to make XMLSpy to catch all errors at once. If XMLSpy does not have this feature, what software out...
2
by: shk253 | last post by:
Hi all - The following is the code for one of my functions in a program but the compiler is reporting errors. I've also tried declaring the variable temp within the function but the number of errors...
88
by: Bill Cunningham | last post by:
Would anyone be interested in giving this a quick look. The compiler gave me so many errors I don't know where to start. This is my first use of isalpha. #include <stdio.h> #include <stdlib.h>...
37
by: Prisoner at War | last post by:
Actually, it doesn't have to be a blockquote...but I'm at my wits' end: I want to make bold several lines of text which have a pair of <br /tags between them...seems like the <b></bdo not "carry...
2
dlite922
by: dlite922 | last post by:
Hello Hello! I'm trying to connect to a host that accepts UDP on a port I setup and tested from a command line too called udp_write. However a PHP fsocketopen() call doesn't work nor does it give...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.