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

problem with validation script

P: n/a
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;
}
Jul 20 '05 #1
Share this Question
Share on Google+
5 Replies


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

Jul 20 '05 #2

P: n/a
THANK YOU, Lee!!!

Kathy

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3

P: n/a
Lee wrote on 24 Nov 2003:

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

--
Michael Winter
M.******@blueyonder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #4

P: n/a
On Mon, 24 Nov 2003 22:42:34 GMT
Michael Winter <M.******@blueyonder.co.uk.invalid> wrote:
<snip>
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).


I agree. I always prefer the obvious to non-intuitive idioms.

--
Then there was the man who drowned crossing a stream with an average
depth of six inches.
-- W. I. E. Gates
Jul 20 '05 #5

P: n/a
JRS: In article <Xn*******************************@193.38.113.46 >, seen
in news:comp.lang.javascript, Michael Winter <M.******@blueyonder.co.uk.
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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.