By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,934 Members | 1,677 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,934 IT Pros & Developers. It's quick & easy.

Form validation doesn't work in Netscape 7.1

P: n/a
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>

*********************************************
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In article <ac**************************@posting.google.com >, 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

Jul 23 '05 #2

P: n/a
On 19 Aug 2004 13:24:12 -0700, lmeng <me*******@hotmail.com> 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.
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.