Form validation doesn't work in Netscape 7.1

Discussion in 'Javascript' started by lmeng, Aug 19, 2004.

  1. lmeng

    lmeng Guest

    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(myField.value);
    if ( (iValue >= parseInt(minValue)) && (iValue <= parseInt(maxValue))
    )
    {
    return (true);
    }
    else
    {
    message = "You have entered an invalid value. Please re-enter an
    integer value";
    }

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

    }


    <FORM NAME="pbscConfig" METHOD="POST">

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

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

    <input TYPE="SUBMIT" NAME="modifyPbsc" VALUE="MODIFY" >

    </FORM>

    *********************************************
     
    lmeng, Aug 19, 2004
    #1
    1. Advertising

  2. lmeng

    kaeli Guest

    In article <>, menglei00
    @hotmail.com enlightened us with...
    >>

    > It works well in IE6.0 and Netscape 4.75.


    Then it's correcting a problem. ;)

    >
    > <FORM NAME="pbscConfig" METHOD="POST">


    <form name="someName" method="post" onSubmit="return myValidator(myargs)">

    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?
    http://www.ipwebdesign.net/wildAtHeart
    http://www.ipwebdesign.net/kaelisSpace
     
    kaeli, Aug 19, 2004
    #2
    1. Advertising

  3. On 19 Aug 2004 13:24:12 -0700, lmeng <> wrote:

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


    Welcome.

    > 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.


    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="return 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.

    > Anything special in Netscape 7.1 for handling Javascript?


    No. The fact that it had an effect on *any* browser is a surprise.

    > function inRangeInteger(myField, minValue, maxValue)
    > {
    > var iValue;
    > var message;
    >
    > iValue = parseInt(myField.value);


    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.value)) {
    // 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;

    > if ( (iValue >= parseInt(minValue)) && (iValue <= parseInt(maxValue))


    I'll mention this below.

    [snip]

    > <FORM NAME="pbscConfig" METHOD="POST">


    Valid HTML requires that FORM elements have an action attribute. You can't
    submit a form otherwise.

    > <input type="TEXT" name="text1" onChange="inRangeInteger(this, '1',
    > '60')">
    >
    > <input type="TEXT" name="text2" onChange="inRangeInteger(this, '1',
    > '23')">


    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="inRangeInteger(this, 1, 60);">

    Hope that helps,
    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
     
    Michael Winter, Aug 19, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brett Porter

    Form validation doesn't work

    Brett Porter, Feb 3, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    405
    Brett
    Feb 3, 2004
  2. raagz
    Replies:
    5
    Views:
    413
    Peter Blum
    Apr 17, 2004
  3. Replies:
    1
    Views:
    397
    Andrew Thompson
    Jan 13, 2005
  4. AnnMarie
    Replies:
    7
    Views:
    223
    AnnMarie Caruso
    Nov 24, 2003
  5. bnp
    Replies:
    4
    Views:
    344
Loading...

Share This Page