newbie: some errors reported by validator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • r_ahimsa_m@poczta.onet.pl

    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.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);
    }
  • Doug Miller

    #2
    Re: newbie: some errors reported by validator

    In article <g3bibj$1vs$1@n ews2.task.gda.p l>, r_ahimsa_m@pocz ta.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?

    Comment

    • r_ahimsa_m@poczta.onet.pl

      #3
      Re: newbie: some errors reported by validator

      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_Pol and 301, agencies_in_reg ions 309,
      agencies_per_va riable_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.anno uncement.agency _wholecountry.c hecked === true
      && !AgencyRe...

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

      if (ValidateClient () === true && ValidatePropert y() === true &&
      ValidateAnno...

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

      if (ValidateClient () === true && ValidatePropert y() === true &&
      ValidateAnno...

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

      if (ValidateClient () === true && ValidatePropert y() === true &&
      ValidateAnno...

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

      if (ValidateClient () === true && ValidatePropert y() === true &&
      ValidateAnno...

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

      ClearAgencyRegi ons();
      ---

      I don't understand why ClearAgencyRegi ons() 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/

      Comment

      • Dr J R Stockton

        #4
        Re: newbie: some errors reported by validator

        In comp.lang.javas cript message <g3bibj$1vs$1@n ews2.task.gda.p l>, Wed,
        18 Jun 2008 21:57:04, r_ahimsa_m@pocz ta.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.demo n.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(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;
        >}
        That can be done very much more simply; see via
        <URL:http://www.merlyn.demo n.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 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 = "";
        >}
        That would be nicer as

        function ClearAnnounceme nt()
        { var da = document.announ cement
        da.price.value = "";
        da.valid_from.v alue = "";
        da.valid_to.val ue = "";
        da.offer_type.v alue = "";
        da.price_for.va lue = "";
        }
        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.demo n.co.uk/news-use.htm: about usage of News.
        No Encoding. Quotes precede replies. Snip well. Write clearly. Mail no News.

        Comment

        Working...