JavaScript Form Validation Doesn't Work In Netscape

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AnnMarie

    JavaScript Form Validation Doesn't Work In Netscape

    My JavaScript Form Validation doesn't work at all in Netscape, but it
    works fine in IE.

    I made some of the suggested changes which enabled it to work in IE.
    I couldn't make all the changes because then it didn't work in IE.

    How can I enable this javascipt form validation to work in Netscape?
    When I use netscape, none of the alert boxes appear. It submits the
    form without validating anything.

    In IE, I get all the alert boxes and everything is validated.

    Thanks.

    The relevant code is:

    <script language="JavaS cript" type="text/javascript">
    <!--
    function validate(theFor m)
    {
    var validity = true; // assume valid
    // If user doesn't enter required name, alert them
    if(frmComments. name.value == '' && validity == true)
    {
    alert('Your full name is required.\nPlea se enter your full
    name!');
    validity = false;
    frmComments.nam e.focus();
    return false;
    }
    // If user doesn't enter required company, alert them
    if(frmComments. company.value == '' && validity == true)
    {
    alert('Your company name is required.\n' +
    'Please enter the name of your company!');
    validity = false;
    frmComments.com pany.focus();
    return false;
    }
    // If user doesn't enter the correct email format alert them
    // Email is not required
    var pattern1 = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4 })+$/;
    if (!pattern1.test (frmComments.em ail.value) && validity == true)
    {
    alert('Invalid E-mail Address!\n' +
    'Please re-enter.')
    frmComments.ema il.focus();
    validity = false;
    return false;
    }
    // If user doesn't enter the correct zip code format alert them
    // Zip Code is not required
    if(!frmComments .zip.value == '' )
    {
    var pattern2 = /^\d{5}$/ ;
    if(!pattern2.te st(frmComments. zip.value))
    {
    if(!pattern2.te st(frmComments. zip.value) && validity ==
    true)
    {
    alert('Invalid Zip Code!\nMust be in form 12345.\n' +
    'Please re-enter.');       
    frmComments.zip .focus();
    validity = false;
    return false;
    }
    }
    }
    // If user doesn't enter the correct phone number format alert
    them
    // Phone Number is not required
    if(!frmComments .phone.value == '')
    {
    var pattern3 = /\d{3}\-\d{3}\-\d{4}/;
    if(!pattern3.te st(frmComments. phone.value))
    {
    if(!pattern3.te st(frmComments. phone.value)&& validity ==
    true)
    {
    alert('Invalid Phone Number!\nMust be in form
    555-555-5555.\n' +
    'Please re-enter.');       
    frmComments.pho ne.focus();
    validity = false;
    return false;
    }
    }
    // If data entered is valid, confirm submission
    if(validity == true)
    return formSubmit();
    }

    // Confirm submission
    function formSubmit()
    {
    return ( confirm('Are you sure you want to submit?'));
    frmComments.nam e.focus();
    }

    // Confirm clearing of fields
    function formReset()
    {
    return ( confirm( "Are you sure you want to reset?" ) )
    frmComments.nam e.focus();
    }
    // -->
    </script>

    <form id="frmComments "
    action="http://matrix.csis.pac e.edu/~f03-it604-s03/cgi-bin/comments.pl"
    method="post" onSubmit="retur n validate(this); " onReset="return
    formReset(this) ">

    <input type="submit" align="right" value="Submit" name="submit"
    onchange="frmCo mments.name.foc us();"
    onblur="frmComm ents.name.focus ();">
    <input type="reset" align="right" value="Clear" name="reset">

    The link to this page is:

  • Janwillem Borleffs

    #2
    Re: JavaScript Form Validation Doesn't Work In Netscape


    "AnnMarie" <carusoa@optonl ine.net> schreef in bericht
    news:c659b5f9.0 311231335.740c2 bed@posting.goo gle.com...[color=blue]
    >
    > How can I enable this javascipt form validation to work in Netscape?
    > When I use netscape, none of the alert boxes appear. It submits the
    > form without validating anything.
    >
    > In IE, I get all the alert boxes and everything is validated.
    >[/color]

    IE will accept formName.proper ty, while Netscape expects
    document.formNa me.property.
    [color=blue]
    >
    > The relevant code is:[/color]
    .....[color=blue]
    > function validate(theFor m)
    > {
    > var validity = true; // assume valid
    > // If user doesn't enter required name, alert them
    > if(frmComments. name.value == '' && validity == true)
    > {[/color]

    Replace frmComments with theForm, e.g.:
    if (theForm.name.v alue == '')

    But since name is a reserved property within the Form object, it's saver to
    use the following syntax:
    if (theForm.elemen ts['name'].value == '')


    JW



    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: JavaScript Form Validation Doesn't Work In Netscape

      "Janwillem Borleffs" <jw@jwscripts.c om> writes:
      [color=blue]
      > IE will accept formName.proper ty, while Netscape expects
      > document.formNa me.property.[/color]

      And the W3C DOM specification expects
      document.forms. formName
      or
      document.forms['formName']
      I prefer the second. It works, even if the formName contains special
      characters.

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • AnnMarie Caruso

        #4
        Re: JavaScript Form Validation Doesn't Work In Netscape

        Thanks for your help. I made the suggested changes and it now also
        works in Netscape. I also had to change the form attribute in the form
        tag from id to name. But, name doesn't validate with W3C validator and
        validation is a requirement for my project.

        How can I make an html page with a form validate and work in IE and
        netscape?

        Thanks.





        *** Sent via Developersdex http://www.developersdex.com ***
        Don't just participate in USENET...get rewarded for it!

        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: JavaScript Form Validation Doesn't Work In Netscape

          AnnMarie Caruso <carusoa@optonl ine.net> writes:
          [color=blue]
          > Thanks for your help. I made the suggested changes and it now also
          > works in Netscape.[/color]

          Which Netscape? I guess Netscape 4, since Netscape 6+ will definitly
          work with id.
          [color=blue]
          > I also had to change the form attribute in the form
          > tag from id to name.[/color]

          The id attribute is the recommended one.
          [color=blue]
          > But, name doesn't validate with W3C validator and
          > validation is a requirement for my project.[/color]

          Then you have to change to a DOCTYPE that accepts both, e.g., HTML
          4.01 Transitional. Then add both "id" and "name" attributes to the
          form tag, with identical values.

          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • AnnMarie Caruso

            #6
            Re: JavaScript Form Validation Doesn't Work In Netscape

            Thanks again. It worked. I am using Netscape 7.02. I changed the
            DOCTYPE from 4.0 to 4.01 and used both id and name and it validates and
            works in both netscape and IE.



            *** Sent via Developersdex http://www.developersdex.com ***
            Don't just participate in USENET...get rewarded for it!

            Comment

            • Randell D.

              #7
              Re: JavaScript Form Validation Doesn't Work In Netscape


              "Janwillem Borleffs" <jw@jwscripts.c om> wrote in message
              news:3fc12a1d$0 $138$1b62eedf@n ews.wanadoo.nl. ..[color=blue]
              >
              > "AnnMarie" <carusoa@optonl ine.net> schreef in bericht
              > news:c659b5f9.0 311231335.740c2 bed@posting.goo gle.com...[color=green]
              > >
              > > How can I enable this javascipt form validation to work in Netscape?
              > > When I use netscape, none of the alert boxes appear. It submits the
              > > form without validating anything.
              > >
              > > In IE, I get all the alert boxes and everything is validated.
              > >[/color]
              >
              > IE will accept formName.proper ty, while Netscape expects
              > document.formNa me.property.
              >[color=green]
              > >
              > > The relevant code is:[/color]
              > ....[color=green]
              > > function validate(theFor m)
              > > {
              > > var validity = true; // assume valid
              > > // If user doesn't enter required name, alert them
              > > if(frmComments. name.value == '' && validity == true)
              > > {[/color]
              >
              > Replace frmComments with theForm, e.g.:
              > if (theForm.name.v alue == '')
              >
              > But since name is a reserved property within the Form object, it's saver[/color]
              to[color=blue]
              > use the following syntax:
              > if (theForm.elemen ts['name'].value == '')
              >
              >
              > JW
              >
              >
              >[/color]

              I'm a newbie with js but had a look at the original post and wondered if you
              folks were refering to the fact that the <FORM> tag did not contain a NAME
              attribute... It should, true?

              randelld


              Comment

              • AnnMarie Caruso

                #8
                Re: JavaScript Form Validation Doesn't Work In Netscape

                The form tag should contain the name attriubute. The problem I had was
                that it didn't validate with W3C validator. So I changed the DOCTYPE to
                4.01 and included the name and id attribute and then it validated. I
                hope this helps.



                *** Sent via Developersdex http://www.developersdex.com ***
                Don't just participate in USENET...get rewarded for it!

                Comment

                Working...