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

Form data to db with validation

Status
Not open for further replies.

auger

Technical User
Oct 27, 2001
48
CA
I have a form with a vb validation script. I'm close but it's still not quite running as I want. Details are as follows:

<form name=&quot;frmMyForm&quot; method=&quot;post&quot; action=&quot;action.asp&quot;>

'selects & radio groups snipped here...

<input type=&quot;submit&quot; name=&quot;Cmd_Validate&quot; value=&quot;Submit&quot;>

<%

response.write &quot;<script Language = &quot;&quot;VBScript&quot;&quot;>&quot; & VbCrlf

response.write &quot; <!-- &quot; & vbcrlfs
response.write &quot;'Form Validation for form frmMyForm&quot; & VbCrlf
response.write &quot;sub Cmd_Validate_onclick()&quot; & VbCrlf

dim farray(22,1)

'declare the array variables
farray(0,0) = &quot;selectItem1&quot;
farray(0,1) = &quot;Please Select Item 1&quot;

farray(1,0) = &quot;selectItem2&quot;
farray(1,1) = &quot;Please Select Item 2&quot;

etc...

farray(22,0) = &quot;radioLastItem&quot;
farray(22,1) = &quot;Please select Last Item&quot;

'validation code for the page

for i = 0 to ubound((farray),1)

'values for the select boxes
if i < 3 then
response.write &quot;if frmMyForm.&quot; & farray(i,0) & &quot;.value = 0 then&quot; & VbCrlf
response.write &quot;msgbox(&quot;&quot;&quot; & farray(i,1) & &quot;&quot;&quot;)&quot; & VbCrlf
response.write &quot; frmMyForm.&quot; & farray(i,0) & &quot;.focus &quot; & VbCrlf
i = i + 1
response.write &quot;elseif frmMyForm.&quot; & farray(i,0) & &quot;.value = 0 then&quot; & VbCrlf
response.write &quot;msgbox(&quot;&quot;&quot; & farray(i, 1) & &quot;&quot;&quot;)&quot; & VbCrlf
response.write &quot; frmMyForm.&quot; & farray(i,0) & &quot;.focus &quot; & VbCrlf
i = i + 1
response.write &quot;elseif frmMyForm.&quot; & farray(i,0) & &quot;.value = 0 then&quot; & VbCrlf
response.write &quot;msgbox(&quot;&quot;&quot; & farray(i,1) & &quot;&quot;&quot;)&quot; & VbCrlf
response.write &quot; frmMyForm.&quot; & farray(i,0) & &quot;.focus &quot; & VbCrlf
i = i + 1
else

end if

'values for the radios

response.write &quot;elseif frmMyForm.&quot; & farray(i,0) & &quot;(0).checked = false and frmMyForm.&quot; & farray(i,0) & &quot;(1).checked = false and frmMyForm.&quot; & farray(i,0) & &quot;(2).checked = false then&quot; & VbCrlf
response.write &quot;msgbox(&quot;&quot;&quot; & farray(i,1) & &quot;&quot;&quot;)&quot; & VbCrlf
response.write &quot; frmMyForm.&quot; & farray(i,0) & &quot;(2).focus &quot; & VbCrlf


next

response.write &quot;end if &quot; & VbCrlf
response.write &quot;end sub&quot; & VbCrlf

response.write &quot; //-->&quot; & Vbcrlf
response.write (&quot;</script>&quot;)

%>

</form>

If all of my form fields are filled in, my form data posts to my db.

If I leave blank fields, the validation script kicks in and prompts with an alert. When I OK the alert, focus should go to the missing field...it does not. Instead, the form attempts to post but errors out because I am missing field data.

Can anyone see how that might be accomplished?

Thanks in advance.
 
Wow, you really make form validation complex!

<script language=&quot;vbs&quot;>
sub checkForm()
mess = &quot;&quot;
for x = 0 to document.myForm.elements.length - 1
if document.myForm.elements(x).value = 0 OR document.myForm.elements(x).value = &quot;&quot; then
mess = mess & vbcrlf & document.myForm.elements(x).name
end if
next
if mess <> &quot;&quot; then
alert &quot;The following are incorrect&quot; & mess
else
document.myForm.method = &quot;post&quot;
document.myForm.action = &quot;action.asp&quot;
document.myForm.submit()
end if
end sub
</script>

<form name=&quot;myForm&quot;>

<input type=&quot;button&quot; value=&quot;Submit&quot; onClick=&quot;checkForm()&quot;
</form> Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048

mikewolf@tst-us.com
 
Thanks mwolf00.

I played with that code & was unable to get the wrinkles out. I couldn't validate and post. I'm not very good at this as you are likely well aware. Ha ha. Anyway, I know the code I had for validation was lengthy but it worked and I was so close to having this form do everything I need. I also know the code to send my form data to db works so I didn't want to change it too much.

Does anyone know if I can get my single submit button to first validate my form data and then post to my db?
 
Posting to the db is done by a form handler. You can do it on the same page with something like this:

pageName.asp

<%
IF request.servervariables(&quot;REQUEST_METHOD&quot;) = &quot;POST&quot;
sql = &quot;INSERT INTO myTable (PhoneNum) VALUES (&quot;&_
&quot;'&quot; & request(&quot;phoneNum&quot;) & &quot;')&quot;
objCN.execute(sql)
response.write &quot;Data Inserted&quot;
response.end
END IF
%>

<script>
function subForm(){
mess = &quot;&quot;
if (document.myForm.phoneNum.value == &quot;&quot;){
mess += &quot;\n- Phone Number&quot;
}
if (mess != &quot;&quot;){
alert (&quot;Please Fill The Following and Resubmit&quot; + mess)
}
else{
document.myForm.method = &quot;post&quot;
document.myForm.action = &quot;pageName.asp&quot;
document.myForm.submit()
}
}
</script>

<form name=&quot;myForm&quot;>
<input name=&quot;phoneNum&quot;>
<input type=button value=&quot;Submit&quot; onClick=&quot;subForm()&quot;>
</form> Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048

mikewolf@tst-us.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top