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

Form check 1

Status
Not open for further replies.

nonprogrammer

Technical User
Dec 28, 2005
143
US
Hello everyone,

I was wondering if I could get some help. for the past couple of weeks I have been battling with an asp script that I got to work over the weekend. In the Form I have 3 radio buttons and a input field.
then with the asp script it populates the Form this is to select different parts I guess the easiest way to explain it is it looks kind of like a football pool script when you can pick points, that is where I took the idea and adapted it to my parts and meassures. The help that I need is validating the form. If a user entes 3/4 or any number twice or more than twice, to give me a warning. So if I enter 5 in the first input and 6 in the second and 5 in the third and 6 in the fourth, it will give me a warning saying I have entered 5 more than once, then when I enter a number other than five but still keep the 6 twice that it warns me for that next.
I am a bit lost on this one so help is more than gratly appreciated.

Thanks in advance
 
Do you submit the form after each entry?

What do the radio buttons do?

Where do you store the entries during the process of picking? I mean do you have a field for each pick, or do you keep track of the picks in a database? Or some other process.
 
the radio buttons are to select the category, all the values are submited to an access db

so it would look something like this


radio button Knobs radio button Handles Radio Button Cases
then input field (here you can input a number from 1 to 25



 
So it is like this.

Click a radio button. For example, Handles.
Enter a number in the input field, e.g., 5.

Click Submit to submit the form.

Store 5 and Handles in Access along with an ID for this person.

Rebuild the form.

Click another radio button. For example, Cases.
Enter a number in the input field, e.g., 5.

Click Submit to submit the form.

Check the database for this person to see whether they have entered 5 already.

If so then display an error message and rebuild the form.


Is that the process?
 
rac2, that is the process. the only difference is that you the the radio buttons and the input are all dynamic but there can be multiple depending on the number of product categories for example
Day 1
radio raido radio input
knobs handles cases enter a number from 1 to 25
knobs handles cases enter a number from 1 to 25
knobs handles cases enter a number from 1 to 25
knobs handles cases enter a number from 1 to 25
knobs handles cases enter a number from 1 to 25
knobs handles cases enter a number from 1 to 25
knobs handles cases enter a number from 1 to 25
knobs handles cases enter a number from 1 to 25

or it could just be
Day 2

radio raido radio input
knobs handles cases enter a number from 1 to 25
knobs handles cases enter a number from 1 to 25
knobs handles cases enter a number from 1 to 25
knobs handles cases enter a number from 1 to 25

Wht I really need is to check that the input is not the same number twice The Number can be in day1 and in day2 but it cannot be twice in day1 or twice in day2
 
Form validation can be done in Javascript when a submit button is clicked. This is used to check the data entered into the current form. These checks deal with things like no data in required fields, non-numeric values in fields that must be numbers, conditions like if box A is checked then box B must not be checked and so forth. If the validation fails an alert box is displayed with advice about the problem and the form is not submitted.

After the form is submitted the data can be checked also. This can be for the same kinds of issues. Or additional issues involving comparing the submitted data to historical records and contextual information.

For example, the person has already picked 5 handles today, they can pick 6 handles or 6 cases but they cannot pick 5 handles, 5 cases, nor 5 knobs.

Programmer-types sometimes refer to the first kind of validation as client-side validation because Javascript runs on the persons computer, not on the web server. The second kind might be called server-side validation. An important point about the difference is that server-side validation can use information stored in databases, client-side validation does not have access to the database.

Your requirement sounds like you need to use server-side validation after the form is submitted. Therefore you need to add code to the ASP script to perform the validation and prepare the error message.

Here is an outline of the process.
Code:
<%
Dim msgError
'Variables used in query to build dynamic form
Dim strQueryPicks, rsQueryPicks

'Variables used in query to obtain history for a person
Dim strQuery, rsQuery, etc
Dim personid
...

If Request.Form("Submit") = "Store My Pick" Then
   'Set up the ADODB stuff to query the database
   ...
   'General form of validation query
   strQuery = "SELECT part, measure FROM MyTable WHERE "
   strQuery = strQuery & "personid = " 
   strQuery = strQuery & Request.Form("personid") & " AND "
   strQuery = strQuery & "date_of_pick = CDate ( Now () ) " 
   
   'Execute the query to obtain a recordset with the data 
   ' for this person on this day
   ...
   'Loop through the recordset and compare the pick today
   '  with the history of picks.
   msgError = ""
   Do Until rsQuery.EOF
      If rsQuery("pick") = Request.Form("pick") And 
         rsQuery("measure") = Request.Form("measure") Then
         
         msgError = "Please enter a different measure.
      End If
      
      rsQuery.MoveNext
   Loop
End If

...

'Existing code to store the submitted data.
'But first check whether there is a duplicate.
If Not msgError = "" Then
   'Store the submitted data.
   ...
End If

...

'Existing code to build the dynamic form
personid = rsQueryPicks("personid")
%>
<html>
...
<h1>My Pick-of-the-Day</h1>

<div class="errormessage"><%= msgError %></div>

<form action="pick_of_the_day.asp">
<% 'Build dynamic form %>
<input type="submit" value="Store My Pick">
<input type="hidden" value="<%= personid %>">
</form>
...
</html>

What is happening here.

You will add a block of code at the top of your ASP script to perform the validation that involves previous picks. This block of code is the If Then ... End If block which I sketched above.

It starts by checking whether the form was submitted or not. When someone first goes to the page, pick_of_the_day.asp, there is no value for Request.Form("Submit") so the block is skipped. After submiting the form the block is executed.

The validation requires a new query of the database to obtain the historical values involved in the validation. This will be a second query different from the one used to build the dynamic form.

Then step through the recordset comparing each value to the submitted values. If everything is OK, the error message will remain blank. If a match is found the error message will be created. There is a place in the HTML to display the message on the page. It is always displayed but usually it is blank.

And finally do not store the submitted data if there was an error. Skip that code if the error message is not blank.

Note that you will need to loop through the form items also. My code does not do that. That loop can be inside the recordset loop, or outside, it doesnt really matter.

Also I am not terribly familiar with Access, so the condition on the date in the WHERE clause may not be correct.
 
rac2,
I really appreciate all the help and all your advice has helped me understand asp a bit more. But I still think it should be a javascript because I need to check the data before it goes into the database. The first part sounds just like waht I need
"Form validation can be done in Javascript when a submit button is clicked. This is used to check the data entered into the current form. These checks deal with things like no data in required fields, non-numeric values in fields that must be numbers, conditions like if box A is checked then box B must not be checked and so forth. If the validation fails an alert box is displayed with advice about the problem and the form is not submitted."

I really appreciate all the help.
 
Glad to help.

Just to clarify, then I will sign off.
...I need to check the data before it goes into the database...

Both client-side and server-side do that.

The stages in the process are

1. First build dynamic form.
2. Click Submit button on form.
3. Client-side validation.
4. Send form data to server.
5. Server-side validation.
6. Store data in database.
7. Respond with anther web page such as
a confirmation page,
an error page,
the dynamic form with the fields filled in as they were submitted, or
an empty form ready for new data.

Good luck and remember this, most good programming is done over the weekend and after midnight.
 
nonprogrammer, using Javascript to do client-side validation is useful in that you can prevent the form from submitting if the data is not what you expect and perform other functions prior to the data submitting to your processing page. But since the code is executing client-side it is unsecure and could be messed with by someone with the intent of bypassing your tests or even to hack into your or wreck your data.

So while it is a good idea to do pre-submission validation it is still important to test all the values server-side prior to submitting them to the database. It would be very easy for someone to hack your data by creating their own copy of the client-side form and submitting invalid data to your asp page submitting actual SQL code through a form field causing your queries to do something other than what was intended. That is why it is important to do server-side validation as well.


At my age I still learn something new every day, but I forget two others.
 
Noted, and appreciated, but the scripts that I am writing are for a small company and it is used only in their intranet. Only autenticated users can access it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top