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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Correct Way to Check if Form Fields Left Blank

Status
Not open for further replies.

Jables

Programmer
Aug 28, 2001
148
0
0
US
I am trying to check and see if a user is submitting a form with any blank fields.

I've tried the following:

1. If Isnull(Request.Form(field_name)) then
Response.Redirect(URL_BlankField)
End If

2. If Request.Form(field_name) = "" then
Response.Redirect(URL_BlankField)
End If

3. If CStr(Request.Form(field_name)) = "" then
Response.Redirect(URL_BlankField)
End If

With all three I get an internal server error when the form submits. I'm using a form that POSTs to itself and executes the above code upon submission.

Am I just making a simple syntax error, or is something else wrong here?


 
Nevermind. I'm an idiot. Finally realized I was typing Request.Form(username) instead of Request.Form("username"). Ain't that always the case? Something simple trips you up for hours.
 
That's good you fixed your problem...a note though...

Why are you checking if the user input data into the field after they already clicked on the submit button and the page submits? That is making an unnecessary trip to the server which is bad if the user has a slow connection or the server is really busy. I suggest using javascript on the client-side to verify if there is data input into the field before the page is even submitted:


<html>
<head>
<title>Form with verification page</title>
<script language=&quot;javascript&quot;>
<!--
function checkForm(form){
if(form.firstName.value==&quot;&quot;){
alert(&quot;Enter your first name.&quot;);
form.firstName.focus();
return false;
}
if(form.lastName.value==&quot;&quot;){
alert(&quot;Enter your last name.&quot;);
form.lastName.focus()
return false;
}
else{return true;}
}
// -->
</script>

</head>
<body>

<Form onSubmit=&quot;return checkForm(this)&quot; action=&quot;nextpage.asp&quot; method=&quot;post&quot;>
First: <input type='text' name='firstName'><BR>
Last: <input type='text' name='lastName'><BR>
<input type='submit' value='Submit'><BR>
</Form>

</body>
</html>


This will check to see if there is a first and last name input into the fields before the page is ever submitted. If one of them is empty, it'll produce an alert to the user informing them to enter the information, and will not submit the form.

You can also make sure there is a minimum ammount of char's entered by using:
if(form.firstName.value.length <= 3){...
And there's a few more tricks, but this should get you started and make your pages not only look more professional, but also save on processing time for both the user and the server!

Hope this helps... -Ovatvvon :-Q
 
Though there is something to be said for testing both sides - client and server, allowing for users that manage to send the details outside the check (for example, should javascript be switched off)?

cheer
m Mark Saunders :)
 
Well, you could certainly put both on there, one on the server side to just in case they have their javascript disabled, or you could run a check for that when they first hit your site. Ya, you could put both for &quot;just in case&quot;...how many people do you know that actually have their javascript disabled in their browser though? I'm curious. -Ovatvvon :-Q
 
You should always revalidate your input on the server side using code that the user can't access. Chapters.ca lost alot of money in the first generation of their website when they did all their validation on the client side. The site would actually post the per item price to the client, then accept it back as the price and charge the credit card accordingly when the form was submitted. Once could download the page, change the price to $0.01 and then post to their site and get everything for a penny a piece.
 
hmm, I think that would just be pretty stupid to allow the user to input the price of a product. That should be somthing that is auto inserted from their database. That was their fault. -Ovatvvon :-Q
 
But then you could say it is pretty stupid not to double check when the overhead for doing so is so little?

I think you would be surprised by the number of people that do have their JavaScript switched off - hardly surprising when one half of the community is proclaiming &quot;well if you would be so stupid as to leave JavaScript on then you deserve a virus&quot;

>:-<
 
don't think anyway 'deserves' a virus for leaving javascript on. What's the point of having it if you don't use it? That's like saying you deserve an accident for driving your car you have parked in your garage!

Also, it's not like saying that &quot;it is pretty stupid not to double check when the overhead for doing so is so little&quot;. Your checking for proper input's into the fields like name, address, etc. But when the product is selected by the user, everything should automatically be inserted from the database, weight, size, nomenclature, price, etc, etc. One it keeps the user from having to do it, and two, it keeps the user from making changes. That's just business. That's just common sense.

=)
-Ovatvvon :-Q
 
Thanks to everyone for all the replies. Yes, I had considered the javascript option before trying to do it server side. I decided on server side for a few reasons:

1. Selfish reason. I'm more comfortable writing code in VBScript. I'm more familiar with the functions available. I know better what I can do with it. There are about 5 checks I need to perform on the form. I know how to do all of them in VBScript. I can do two of them in Javascript. Javascript is more C-sish, a language that was the thorn in my side for a lot of years. It's just too low-level for an idiot like me.

2. My site doesn't really have a lot of traffic. If I had a ton of traffic, my first worry would be that I'm using MS Access as the database, not form validation.

3. I've tested it with a 56k modem connected at something like 30k, and it doesn't take a painfully long time.

4. The server side validation just seems more solid to me. I like knowing exactly what's coming into my database without any bizarre client-side twists.

Well, I guess all the above are justifications for reason Number 1. Anyway, thanks for all the suggestions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top