Solved

Number validation vbscript

Posted on 2007-12-03
8
2,011 Views
Last Modified: 2010-04-21
I've got an old school web page that was built for us many years back. It has an insert page and a submit page that validates some fields and then fowards them to our sql server. Currently it validates if the field is filled in with text or numeric, but I need it to validate for numerics only. Below is the actual text where the validation occurs. Any help would be appreciated.

Dim annual_quantity_n
annual_quantity_n = Replace(Request("annual_quantity_n"),"'","''")
Validate annual_quantity_n,"Annual Quantity"

Zach
0
Comment
Question by:zneria
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 20401229
try use IsNumeric function to check whether a value is numeric or not, like:

...
if isnumeric(yourValue) = false then
    msgbox "The value is not numeric"
    exit sub
end if
..
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 20401235
if is in ASP, something similar:

...
if isnumeric(yourValue) = false then
    response.write "The value is not numeric"
  '...
end if
..
0
 
LVL 28

Accepted Solution

by:
sybe earned 250 total points
ID: 20401842
The Classic ASP function IsNumeric fails in 2 instances:

If the parameter is Null, then it returns True, which obviously is not what you want and if the typename is Decimal (which happens sometimes with values reurned from a database), it returns False, which again is the wrong result. See below for a replacement function.

I also suspect that you need an Integer (although I am not sure).

================================

> if isnumeric(yourValue) = false then

I find this a quite remakarble line. Normally one would write

If Not IsNumeric(somevalue) Then

=============================






If IsNumeric2(Request("annual_quantity_n")) Then
    AnnualQuantity = Clng(Request("annual_quantity_n"))
Else
   ' not a numeric value, do what you need to do
 
End If
 
 
Function IsNumeric2(ByVal var)
    Select Case VarType(var)
        Case 0: IsNumeric2 = False
        Case 14: IsNumeric2 = True
        Case Else: IsNumeric2 = IsNumeric(var)
    End Select
End Function

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 50

Expert Comment

by:Ryan Chong
ID: 20401892
>>Normally one would write..

that's depends on how a developer write the syntax, which basically both working fine...
0
 
LVL 20

Expert Comment

by:jitganguly
ID: 20404236
If its user input you should have clent side Javascript validation

http://www.google.com/search?q=numbers+only++with+isNaN&hl=en&safe=active
0
 
LVL 1

Author Comment

by:zneria
ID: 20404395
Sybe I'll go ahead and try this out on the submit page. Jitganguly I'm aware of the javascript however I didn't build these pages and I need it to be vbscript on the submit page, not the input page. Thanks for the 411 though.
0
 
LVL 20

Expert Comment

by:jitganguly
ID: 20404414
>>Thanks for the 411 though.
:-). It was 555 1212 :-)

Good luck
0
 
LVL 1

Author Closing Comment

by:zneria
ID: 31412536
Sybe,
The code worked like a charm. Can't thank you enough man =)

Zach
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question