Form validation doesn't work in Netscape 7.1

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

    #1

    Form validation doesn't work in Netscape 7.1

    Hi,

    I am new to this Forum. Thanks in advance for any kind help.

    In the following HTML code, when I change the value of one text field
    then click "Modify" button, if the validation fails a message will
    popup and the cotent of the form should NOT be submitted. (The actual
    code connects to the database at the backend so I can check if the
    value is submmited).

    It works well in IE6.0 and Netscape 4.75.

    But in Netscape 7.1, if the validation fails, the message pop up but
    the form submit still go through. In another word, the return from JS
    function inRangeInteger( ), which is triggered by onChange event of the
    text field, is ignored when the form submit.

    Anything special in Netscape 7.1 for handling Javascript?

    Any response is highly appriciated.

    *************** *************** ********
    function inRangeInteger( myField, minValue, maxValue)
    {
    var iValue;
    var message;

    iValue = parseInt(myFiel d.value);
    if ( (iValue >= parseInt(minVal ue)) && (iValue <= parseInt(maxVal ue))
    )
    {
    return (true);
    }
    else
    {
    message = "You have entered an invalid value. Please re-enter an
    integer value";
    }

    alert(message);
    myField.focus() ;
    return (false);

    }


    <FORM NAME="pbscConfi g" METHOD="POST">

    <input type="TEXT" name="text1" onChange="inRan geInteger(this, '1',
    '60')">

    <input type="TEXT" name="text2" onChange="inRan geInteger(this, '1',
    '23')">
    ....

    <input TYPE="SUBMIT" NAME="modifyPbs c" VALUE="MODIFY" >

    </FORM>

    *************** *************** ***************
  • kaeli

    #2
    Re: Form validation doesn't work in Netscape 7.1

    In article <acbbeeb6.04081 91224.55ccb4bb@ posting.google. com>, menglei00
    @hotmail.com enlightened us with...[color=blue][color=green]
    >>[/color]
    > It works well in IE6.0 and Netscape 4.75.[/color]

    Then it's correcting a problem. ;)
    [color=blue]
    >
    > <FORM NAME="pbscConfi g" METHOD="POST">[/color]

    <form name="someName" method="post" onSubmit="retur n myValidator(mya rgs)">

    That's the only *reliable* way of stopping form submission when you have a
    submit (type=submit) button. Assuming the user has JS, of course.

    If you must check the values onChange of the text, fine, but don't rely on
    that to stop form submission. Have your myValidator function call the same
    code before submission (don't bother with return values in the onChange
    unless you want to clear the text (in some UAs), but from your code, you were
    throwing them away anyway).

    --
    --
    ~kaeli~
    Well, aren't we just a flipping ray of sunshine?



    Comment

    • Michael Winter

      #3
      Re: Form validation doesn't work in Netscape 7.1

      On 19 Aug 2004 13:24:12 -0700, lmeng <menglei00@hotm ail.com> wrote:
      [color=blue]
      > I am new to this Forum. Thanks in advance for any kind help.[/color]

      Welcome.
      [color=blue]
      > In the following HTML code, when I change the value of one text field
      > then click "Modify" button, if the validation fails a message will
      > popup and the cotent of the form should NOT be submitted. (The actual
      > code connects to the database at the backend so I can check if the
      > value is submmited).
      >
      > It works well in IE6.0 and Netscape 4.75.
      >
      > But in Netscape 7.1, if the validation fails, the message pop up but
      > the form submit still go through. In another word, the return from JS
      > function inRangeInteger( ), which is triggered by onChange event of the
      > text field, is ignored when the form submit.[/color]

      Returning false from an onchange handler will do nothing, even if you do
      it correctly (which you haven't). :)

      To return a value properly, there should be a return statement in the
      intrinsic event, too. For example,

      <a ... onclick="return myHandler()">

      function myHandler() {
      return false; // Cancel the click
      // or
      return true; // Allow the click
      }

      An change event won't affect a submission anyway. Only two things will:
      the form's submit event, and the submit button's click event. The former
      is preferred.

      <form ... onsubmit="retur n validate(this)" >

      function validate(form) {
      /* Validate the form using the reference in 'form'.
      *
      * Returning false from this function will cancel
      * the submission.
      */
      }

      Use the change event to validate a value on the control. Use the submit
      event to validate the form as a whole before submission.
      [color=blue]
      > Anything special in Netscape 7.1 for handling Javascript?[/color]

      No. The fact that it had an effect on *any* browser is a surprise.
      [color=blue]
      > function inRangeInteger( myField, minValue, maxValue)
      > {
      > var iValue;
      > var message;
      >
      > iValue = parseInt(myFiel d.value);[/color]

      The parseInt() function should be called with the radix (base) specified.
      Problems can occur otherwise. However, you should check that the value is
      actually a valid number first, preferably with a regular expression. For
      an integer, you can use:

      if(/^([1-9]\d*)|0$/.test(myField.v alue)) {
      // myField contains a valid integer
      // that contains no leading zeros.
      }

      Once you've established that the string represents a number, it's quicker
      to use unary plus (+) to perform the conversion:

      iValue = +myField.value;
      [color=blue]
      > if ( (iValue >= parseInt(minVal ue)) && (iValue <= parseInt(maxVal ue))[/color]

      I'll mention this below.

      [snip]
      [color=blue]
      > <FORM NAME="pbscConfi g" METHOD="POST">[/color]

      Valid HTML requires that FORM elements have an action attribute. You can't
      submit a form otherwise.
      [color=blue]
      > <input type="TEXT" name="text1" onChange="inRan geInteger(this, '1',
      > '60')">
      >
      > <input type="TEXT" name="text2" onChange="inRan geInteger(this, '1',
      > '23')">[/color]

      Rather than passing strings (I'm referring to the second and third
      arguments) and converting them to numbers, why don't you pass numbers?

      <input ... onchange="inRan geInteger(this, 1, 60);">

      Hope that helps,
      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      Working...