Form validation doesn't work in Netscape 7.1

L

lmeng

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>

*********************************************
 
K

kaeli

menglei00 said:
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).

--
 
M

Michael Winter

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top