problem with validation script

K

KathyB

Hi, I need to validate a hi/low range, allowing decimal numbers. For
this example, the low is 1, high is 5, but something is not working. I
got this script from someone helping me in this group, but I must have
done something wrong. When I enter 7, it prompts me as an incorrect
entry, as expected. However, when I enter 22 or 23, etc. the script
passes those entries (perhaps accepting individual numbers between 1
and 5. Sorry, but the regular expression stuff is hardly my strong
suit...

Please tell me where I've gone astray! Thanks, Kathy

function ValidateSave(formRef,fieldName,min,max)var formField =
formRef.elements[fieldName];

if (!/^\d+(\.\d+)?$/.test(formField.value)) {
alert('Invalid entry...please try again!');
formField.focus();
formField.select();
return false;
}
if ((formField.value < min) || (formField.value > max)) {
alert("The entry must be between " +min+ " and " +max+ ".");
formField.focus();
formField.select();
return false;
}
 
L

Lee

KathyB said:
Hi, I need to validate a hi/low range, allowing decimal numbers. For
this example, the low is 1, high is 5, but something is not working. I
got this script from someone helping me in this group, but I must have
done something wrong. When I enter 7, it prompts me as an incorrect
entry, as expected. However, when I enter 22 or 23, etc. the script
passes those entries (perhaps accepting individual numbers between 1
and 5. Sorry, but the regular expression stuff is hardly my strong
suit...

Please tell me where I've gone astray! Thanks, Kathy
if ((formField.value < min) || (formField.value > max)) {

The problem is not with the regular expressions, but with the
comparisons. Field values are strings, and so the comparisons
are being done as string comparisons, and the string "22" is
less than the string "5".

You need to convert the values to integers before comparing.
One simple way to do that is to apply the unary plus sign to
them. Since that operator only applies to numbers, the engine
will automatically convert the values to numbers for you:

if ((+formField.value < min) || (+formField.value > max))
 
M

Michael Winter

Lee wrote on 24 Nov 2003:

You need to convert the values to integers before comparing.
One simple way to do that is to apply the unary plus sign to
them. Since that operator only applies to numbers, the engine
will automatically convert the values to numbers for you:

if ((+formField.value < min) || (+formField.value > max))

Alternatively, you could use the constructor of the Number object:

if (( Number( formField.value ) < min )
|| ( Number( formField.value ) > max )) {
}

I personally prefer the second, though they both result in the same
behaviour. Using Number is a little more readable, and doesn't run
the risk of doing something unintended, like concatenating (if you
placed another argument in front of the plus in Lee's example).

Mike
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
in Michael Winter <[email protected].
invalid> posted at Mon, 24 Nov 2003 22:42:34 :-
Alternatively, you could use the constructor of the Number object:

if (( Number( formField.value ) < min )
|| ( Number( formField.value ) > max )) {
}

Rather than that, I suggest

var T = Number( formField.value ) // or +formField.value
if (T < min || T > max ) {
}

IMHO, once one is accustomed to seeing such as

var T = +formField.value

one will on reading it readily see that it is a means of getting what is
wanted (a number) from what is available (a field, holding a string),
and will not be over-tempted to fiddle with it inappropriately.

AFAICS, a + directly after an assignment operator must be a unary
arithmetic operator, whose only purpose can be conversion to number.
 

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,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top