Solved

Number validation vbscript

Posted on 2007-12-03
8
1,988 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 49

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 49

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
 
LVL 49

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now