Check for decimal

1. Senior Member
Join Date
Dec 1969
Posts
156

## Check for decimal

I am using the following client-side script to check if the data entered is a number and whether it is less than 10. But this check also prevents a number with a decimal value to be input. I would like to let a decimal value (upto 2 decimal places) to be input. How can i do that?<BR><BR>Sub Num(ByVal NumVal, ByVal msg)<BR> <BR> Hdr = "Input Error!"<BR> if not (isNumeric(NumVal) and (Len(NumVal) &#060; 2)) then <BR> validation = false<BR> MsgBox msg, 8, Hdr<BR> end if<BR>end sub

2. Senior Member
Join Date
Dec 1969
Posts
1,024

## RE: Check for decimal

if not (isNumeric(NumVal) and (Len(NumVal) &#060; 2)) then<BR><BR>surely you want to use an if statement to check if theres a decimal place and if so that there are no more than 2 digits before the decimal place and two after it.<BR><BR>Don&#039;t be so lazy, you can do the work yourself!!

3. Senior Member
Join Date
Dec 1969
Posts
156

## Not sure if i understand!

I am not sure if i understand this!<BR><BR>I can see that i have to check for a &#039;.&#039; in the string but how do i do that?

4. Senior Member
Join Date
Dec 1969
Posts
1,032

## RE: Not sure if i understand!

&nbsp;<BR>numerically use something like :- (untested)<BR><BR>Sub Num(NumVal)<BR><BR>if not isNumeric(NumVal) then<BR>alert("not a number")<BR>exit sub<BR>else<BR>if isnumeric(numval) and ( clng(numval)&#062;10 or clng(numval) &#060;0 ) then <BR>alert("number outside range")<BR>end if<BR>end if<BR>end sub<BR><BR>jon

5. Senior Member
Join Date
Dec 1969
Posts
156

## Code i am using works, but....

The code that i am using works, but it prevents me from entering a number with a decimal value also. I want the decimal value to be allowed.

6. Senior Member
Join Date
Dec 1969
Posts
1,032

## RE: Code i am using works, but....

YOUR code rewritten =<BR><BR>Sub Num(ByVal NumVal, ByVal msg) <BR>Hdr = "Input Error!" <BR> if not isNumeric(NumVal) then <BR> validation = false <BR> MsgBox msg, 8, Hdr <BR>exit sub<BR><BR>else<BR>if isnumeric(numval) and ( clng(numval)&#062;=10 or clng(numval) &#060;0 ) then <BR> validation = false <BR> MsgBox msg, 8, Hdr <BR>exit sub<BR>end if<BR>end if <BR>end sub <BR><BR>jon

7. Senior Member
Join Date
Dec 1969
Posts
156

## another problem

ok that works! Thanks<BR><BR>I am now using formatNumber(myNumber, 2) to round of this number to 2 decimal places. But, when i extract this data from the database, it gives me a long number. How can i round it to two decimals?

8. Senior Member
Join Date
Dec 1969
Posts
96,118

## FormatNumber again

Why not?<BR><BR>If you mean "how can I round it to two decimals when it is in the DB", then it depends on the DB. But there isn&#039;t really any reason to do this, in most cases.<BR><BR>

9. Senior Member
Join Date
Dec 1969
Posts
96,118

## WHY would you ever use...

...VBScript to do form validation in the browser?<BR><BR>Now your code is completely ignored by Netscape and other browsers.<BR><BR>Having said that...<BR><BR>What is the point of your use of CLNG in that code???<BR><BR>If the idiot user types in (example) 9918277112 then CLNG will blow up with an error.<BR><BR>CDBL, on the other hand, will accept *any* number that isNumeric gives the nod to.<BR><BR>But, really, this code should be written in JavaScript.<BR><BR>

10. Senior Member
Join Date
Dec 1969
Posts
11,334

## It's been awhile

And I may be in a different language mode as I usually am, but doesn&#039;t it just reset?<BR><BR>In other words (at least in C++) you can loop that sucker forever... so going one over in the short department just leaves you with the negative number on the other side (assuming it&#039;s a signed number)<BR><BR>So, one over 32,768 ( I can&#039;t remember the number off the top of my head) would produce -32,767 on the other side, no?<BR><BR>It&#039;s amazing how much you forget things when ya don&#039;t work with them every day anymore :(

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•