Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validation Of numeric Fields

Status
Not open for further replies.

Skittle

ISP
Sep 10, 2002
1,528
US
Hi,

I have a simple form which accepts a year ( eg. '2002' )
and is then submitted to an asp page for validation.
How can I validate the posted string value is a valid
numeric value? I want '2002' to be accepted but '200 ' or
200a' to be trapped as an error.

I thought it would be something along the lines of:-

If Not Request.Form("Year") Is Numeric
do flag error
End If

 
You are better doing validation client side where possble to reduce round trips to the server. You can use Javascript or VBScript (IE only) to do this. Define a javascript function to validate the fields, call this function with the onSubmit() function in the form tag, and only submit the data if the fields are validated successfully.

This way you can ensure the field length is correct, the field is only numeric, and also that any dates are valid (i.e. disallow 29th Feb) before returning to the server.

There are numerous good websites full of Javascript validation functions if you need further information.
 
jcprince is right, though. But if you wanted to do it with ASP... the code above does it...
 
or why not just have a drop down box, so you dont even need to validate it. Saturday 12.00
im6.gif
im2.gif
im2.gif
20.00
im5.gif
im4.gif
im7.gif
3.00am
im8.gif
Sunday [img http
 
you gotta validate at the server...you have to...there is no way to rely on the client...

1. they may have javascript turned off
2. they may attempt maliciously screw with your app

you can implement basic checks (like are the fields filled out) but you gotta gotta gotta check it on the server before you do something with it...

it is extremely foolish not to do this...my development team relied on clientside check for some of our web applications till errors started to show in the DB because no server side check was being done. It can kill functionality of your app due to bad data and makes you look like an amatuer to your clients...

Bastien

There are many ways to skin this cat,
but it still tastes like chicken
 
another reason for server-side validation is security.

It's easy for hackers to turn off your client-side scripting and post malicious code.
 
Wow thanks everybody.

Lots of good ideas.

I really appeciate it.

Roy
 
once you ensure it is numeric either

ensure it is within a range ie

1950 -> 2050+

or

if len(cstr(iYear)) = 4 then
' Valid
end if

will ensure it is the correct year length (part 2 of your question)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top