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!

onSubmit 1

Status
Not open for further replies.

Berns

IS-IT--Management
Apr 25, 2002
41
US
hello-someone was helping me with this several days ago but I still can't figure it out. This is what I am trying to do:

I have a form with several radio button type questions. Only one of the fields is required. If any of the not required fields are left blank when the user hits submit they will get an alert that says something like "Not all of the questions were answered, do you which to submit? Then the user picks Yes or No. If the user picks yes the form submits, if they pick No it doen not. I'm already using the onSubmit for another function (to validate the required field), can anyone help?

Here is part of the code:
function checkRequired( theForm )
{
var bMissing2 = false;
if ( 0 == theForm.Q15.value )
bMissing2 = true;

if (theForm.Q15.value.length!=6)
{
alert( "Please enter your six digit ID.");
theForm.Q15.focus();
return false;
}
else
{
return true;
}

}

------THEN the Onsubmit:
onsubmit="return checkRequired(this)">
 
function checkRequired(theForm){
//check ID first
if (theForm.Q15.value.length!=6)
{
alert( "Please enter your six digit ID.");
theForm.Q15.focus();
return false;
}
//ID is OK so check for blank questions
var missMess = ""
for (x=1; x<=15; x++){
thisAnswer = eval(&quot;theForm.Q&quot; + x + &quot;.value&quot;)
if (thisAnswer == 0){
missMess += &quot;\n-Question #&quot; + x
}
}
doAnyways = true
if (missMess != &quot;&quot;){
doAnyways = confirm(&quot;The following are blank. Do you wish to submit anyways?&quot; + messMess)
}
return doAnyways;
}
Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048

mikewolf@tst-us.com
 
This isn't working, the form submits without an alert even if only one question was answered.
 
Oh radio buttons... hmm... try this...

function checkRequired(theForm){
//check ID first
if (theForm.Q15.value.length!=6)
{
alert( &quot;Please enter your six digit ID.&quot;);
theForm.Q15.focus();
return false;
}
//ID is OK so check for blank questions
var missMess = &quot;&quot;
//assumes 4 radio groups named Q1,Q2,Q3,Q4
//each question must have more than 1 button in radio group

for (x=1; x <= 4; x++){
thisAnswer = eval(&quot;theForm.Q&quot; + x + &quot;.value&quot;)
answered = false //boolean to search group
for (y=0; y<thisAnswer.length; y++){
if (thisAnswer[y].checked){
answered = true
}
}
if (!answered){
missMess += &quot;\n-Question #&quot; + x
}
}
doAnyways = true
if (missMess != &quot;&quot;){
doAnyways = confirm(&quot;The following are blank. Do you wish to submit anyways?&quot; + messMess)
}
return doAnyways;
}
Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048

mikewolf@tst-us.com
 
i've been trying to figure this our for days now and am super frustrated.

That still doesn't work, the form consists of 14 radio button questions, each with 5 selections (excellent, good, fair, etc) and 4 text field questions (sorry I forgot about these). Does that help? I guess I wasn't very specific initially.

Thanks!
 
It's not hard to alter my code to allow for all of your radio buttons and add the 4 text fields. Post what you have and I'll look at it in the morning. Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048

mikewolf@tst-us.com
 
I tried several thing to make this script work, and I just couldn't get it.

I changed this line several times:
for (x=01; x <= 19; x++){

I changed the &quot;1&quot; to &quot;01&quot; because all of the Q#'s are 2 intergers. then I changed the &quot;4&quot; to &quot;19&quot; to reflect the number of questions that I had.

I may seem like a huge dope here but I just don't get it, I think that I have a bad case of tunnel vision at this point!

Thanks for your help!
 
can you post your whole page?

since all of the Qs are 2 integers you'd have to do this:

for (x=1; x<=19; x++){
if (x<10){
thisAnswer = eval(&quot;theForm.Q0&quot; + x + &quot;.value&quot;)
}
else{
thisAnswer = eval(&quot;theForm.Q&quot; + x + &quot;.value&quot;)
}
//continue on
} Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048

mikewolf@tst-us.com
 
Here's the first part, w/ first question:
<code>
<SCRIPT language=JavaScript>
<!-- Begin
function checkRequired(theForm){
//check ID first
if (theForm.Q15.value.length!=6)
{
alert( &quot;Please enter your six digit ID.&quot;);
theForm.Q15.focus();
return false;
}
//ID is OK so check for blank questions
var missMess = &quot;&quot;
//assumes 4 radio groups named Q1,Q2,Q3,Q4
//each question must have more than 1 button in radio group

for (x=1; x <= 4; x++){
thisAnswer = eval(&quot;theForm.Q&quot; + x + &quot;.value&quot;)
answered = false //boolean to search group
for (y=0; y<thisAnswer.length; y++){
if (thisAnswer[y].checked){
answered = true
}
}
if (!answered){
missMess += &quot;\n-Question #&quot; + x
}
}
doAnyways = true
if (missMess != &quot;&quot;){
doAnyways = confirm(&quot;The following are blank. Do you wish to submit anyways?&quot; + messMess)
}
return doAnyways;
}

var months=new Array(13);
months[1]=&quot;January&quot;;
months[2]=&quot;February&quot;;
months[3]=&quot;March&quot;;
months[4]=&quot;April&quot;;
months[5]=&quot;May&quot;;
months[6]=&quot;June&quot;;
months[7]=&quot;July&quot;;
months[8]=&quot;August&quot;;
months[9]=&quot;September&quot;;
months[10]=&quot;October&quot;;
months[11]=&quot;November&quot;;
months[12]=&quot;December&quot;;
var time=new Date();
var lmonth=months[time.getMonth() + 1];
var date=time.getDate();
var year=time.getYear();
if (year < 2000) // Y2K Fix
year = year + 1900;
document.write(&quot;     &quot; + lmonth + &quot; &quot;);
document.write(date + &quot; &quot; + year + &quot;</center>&quot;);

//number validation
function validate(field) {
var valid = &quot;0123456789&quot;
var ok = &quot;yes&quot;;
var temp;
for (var i=0; i<field.value.length; i++) {
temp = &quot;&quot; + field.value.substring(i, i+1);
if (valid.indexOf(temp) == &quot;-1&quot;) ok = &quot;no&quot;;
}
if (ok == &quot;no&quot;) {
alert(&quot;Your ID must be numeric&quot;);
field.focus();
field.select();
}
}

// End -->

</SCRIPT>
</head>

<font face=&quot;arial&quot; size=&quot;-1&quot; color=&quot;#293163&quot;>
<p><h3>Satisfaction Survey</h3></p>

<p>
Our strategy is to provide high quality service and value to our customers. To participate, please select the choices that best reflect your opinions.</b>.
</p>


<form method=&quot;post&quot; action=&quot; onsubmit=&quot;return checkRequired(this)&quot;>
<INPUT type=&quot;hidden&quot; NAME=&quot;form_version&quot; VALUE=&quot;1&quot;/>
<INPUT type=&quot;hidden&quot; NAME=&quot;source_site&quot; VALUE=&quot;xxx&quot;/>
<INPUT type=&quot;hidden&quot; NAME=&quot;thankyou&quot; VALUE=&quot;<INPUT type=&quot;hidden&quot; NAME=&quot;thankyou_page&quot; VALUE=&quot;&quot;/>
<INPUT type=&quot;hidden&quot; NAME=&quot;fieldorder&quot; VALUE=&quot;Q01,Q02,Q03,Q04,Q05,Q06,Q07,Q08,Q09,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19&quot;/>
<p>
Please enter your ID <b>U</b><input type=&quot;text&quot; maxlength=&quot;6&quot; size=&quot;5&quot; name=&quot;Q15&quot; onBlur=&quot;validate(this)&quot;>
<p><b>HEADER</b><br>
1. Q1<br>
<table border=0>
<tr><Td width=150><font face=&quot;arial&quot; size=&quot;-1&quot;> </td><td width=75><center><font face=&quot;arial&quot; size=&quot;-1&quot;>Excellent</td><td width=150><center><font face=&quot;arial&quot; size=&quot;-1&quot;>Very Good</td><td width=75><center><font face=&quot;arial&quot; size=&quot;-1&quot;>Good</td><td width=75><center><font face=&quot;arial&quot; size=&quot;-1&quot;>Fair</td><td width=75><center><font face=&quot;arial&quot; size=&quot;-1&quot;>Poor</td></tr>
<tr><Td width=550><font face=&quot;arial&quot; size=&quot;-1&quot;><b>Management</b></td><td><center><input type=radio value=&quot;Excellent&quot; name=Q01></td><td><center><input type=radio value=&quot;Very Good&quot; name=Q01></td><td><center><input type=radio value=&quot;Good&quot; name=Q01></td><td><center><input type=radio value=&quot;Fair&quot; name=Q01></td><td><center><input type=radio value=&quot;Poor&quot; name=Q01></td></tr>
<tr><Td width=550><font face=&quot;arial&quot; size=&quot;-1&quot;><b>Payroll Specialist</b></td><td><center><input type=radio value=&quot;Excellent&quot; name=Q02></td><td><center><input type=radio value=&quot;Very Good&quot; name=Q02></td><td><center><input type=radio value=&quot;Good&quot; name=Q02></td><td><center><input type=radio value=&quot;Fair&quot; name=Q02></td><td><center><input type=radio value=&quot;Poor&quot; name=Q02></td></tr>
<tr><Td width=550><font face=&quot;arial&quot; size=&quot;-1&quot;><b>Operation Specialist</b></td><td><center><input type=radio value=&quot;Excellent&quot; name=Q03></td><td><center><input type=radio value=&quot;Very Good&quot; name=Q03></td><td><center><input type=radio value=&quot;Good&quot; name=Q03></td><td><center><input type=radio value=&quot;Fair&quot; name=Q03></td><td><center><input type=radio value=&quot;Poor&quot; name=Q03></td></tr>
<tr><Td width=550><font face=&quot;arial&quot; size=&quot;-1&quot;><b>Training / Compliance Specialist</b></td><td><center><input type=radio value=&quot;Excellent&quot; name=Q04></td><td><center><input type=radio value=&quot;Very Good&quot; name=Q04></td><td><center><input type=radio value=&quot;Good&quot; name=Q04></td><td><center><input type=radio value=&quot;Fair&quot; name=Q04></td><td><center><input type=radio value=&quot;Poor&quot; name=Q04></td></tr>
<tr><Td width=550><font face=&quot;arial&quot; size=&quot;-1&quot;><b>Employee Leasing Co. (EPIX)</b></td><td><center><input type=radio value=&quot;Excellent&quot; name=Q05></td><td><center><input type=radio value=&quot;Very Good&quot; name=Q05></td><td><center><input type=radio value=&quot;Good&quot; name=Q05></td><td><center><input type=radio value=&quot;Fair&quot; name=Q05></td><td><center><input type=radio value=&quot;Poor&quot; name=Q05></td></tr></code>

The alert does not have to specify the questions that were not answered.

Thanks!
 
This works fine - I found some typos (mine)...


<SCRIPT language=JavaScript>
<!-- Begin

function checkRequired(theForm){
//check ID first
if (theForm.Q15.value.length!=6)
{
alert( &quot;Please enter your six digit ID.&quot;);
theForm.Q15.focus();
return false;
}

//ID is OK so check for blank questions
var missMess = &quot;&quot;

for (x=1; x <= 5; x++){
if ( x < 10 ){
thisAnswer = eval(&quot;theForm.Q0&quot; + x )
}
else{
thisAnswer = eval(&quot;theForm.Q&quot; + x )
}
answered = false
for (y=0; y<thisAnswer.length; y++){
if (thisAnswer[y].checked){
answered = true
}
}
if (!answered){
missMess += &quot;\n-Question #&quot; + x
}
}

doAnyways = true
if (missMess != &quot;&quot;){
doAnyways = confirm(&quot;The following are blank. Do you wish to submit anyways?&quot; + missMess)
}
return doAnyways;
}


var months=new Array(13);
months[1]=&quot;January&quot;;
months[2]=&quot;February&quot;;
months[3]=&quot;March&quot;;
months[4]=&quot;April&quot;;
months[5]=&quot;May&quot;;
months[6]=&quot;June&quot;;
months[7]=&quot;July&quot;;
months[8]=&quot;August&quot;;
months[9]=&quot;September&quot;;
months[10]=&quot;October&quot;;
months[11]=&quot;November&quot;;
months[12]=&quot;December&quot;;
var time=new Date();
var lmonth=months[time.getMonth() + 1];
var date=time.getDate();
var year=time.getYear();
if (year < 2000) // Y2K Fix
year = year + 1900;
document.write(&quot; &quot; + lmonth + &quot; &quot;);
document.write(date + &quot; &quot; + year + &quot;</center>&quot;);

//number validation
function validate(field) {
var valid = &quot;0123456789&quot;
var ok = &quot;yes&quot;;
var temp;
for (var i=0; i<field.value.length; i++) {
temp = &quot;&quot; + field.value.substring(i, i+1);
if (valid.indexOf(temp) == &quot;-1&quot;) ok = &quot;no&quot;;
}
if (ok == &quot;no&quot;) {
alert(&quot;Your ID must be numeric&quot;);
field.focus();
field.select();
}
}

// End -->

</SCRIPT>
</head>

<font face=&quot;arial&quot; size=&quot;-1&quot; color=&quot;#293163&quot;>
<p><h3>Satisfaction Survey</h3></p>

<p>
Our strategy is to provide high quality service and value to our customers. To participate, please select the choices that best reflect your opinions.</b>.
</p>


<form method=&quot;post&quot; action=&quot;&quot;; onsubmit=&quot;return checkRequired(this)&quot;>
<INPUT type=&quot;hidden&quot; NAME=&quot;form_version&quot; VALUE=&quot;1&quot;/>
<INPUT type=&quot;hidden&quot; NAME=&quot;source_site&quot; VALUE=&quot;xxx&quot;/>
<INPUT type=&quot;hidden&quot; NAME=&quot;thankyou&quot; VALUE=&quot;<INPUT type=&quot;hidden&quot; NAME=&quot;thankyou_page&quot; VALUE=&quot;&quot;/>
<INPUT type=&quot;hidden&quot; NAME=&quot;fieldorder&quot; VALUE=&quot;Q01,Q02,Q03,Q04,Q05,Q06,Q07,Q08,Q09,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19&quot;/>
<p>
Please enter your ID <b>U</b><input type=&quot;text&quot; maxlength=&quot;6&quot; size=&quot;5&quot; name=&quot;Q15&quot; onBlur=&quot;validate(this)&quot;>
<p><b>HEADER</b><br>
1. Q1<br>
<table border=0>
<tr>
<Td width=150><font face=&quot;arial&quot; size=&quot;-1&quot;></td>
<td width=75><center><font face=&quot;arial&quot; size=&quot;-1&quot;>Excellent</td>
<td width=150><center><font face=&quot;arial&quot; size=&quot;-1&quot;>Very Good</td>
<td width=75><center><font face=&quot;arial&quot; size=&quot;-1&quot;>Good</td>
<td width=75><center><font face=&quot;arial&quot; size=&quot;-1&quot;>Fair</td>
<td width=75><center><font face=&quot;arial&quot; size=&quot;-1&quot;>Poor</td>
</tr>
<tr>
<Td width=550><font face=&quot;arial&quot; size=&quot;-1&quot;><b>Management</b></td>
<td><center><input type=radio value=&quot;Excellent&quot; name=Q01></td>
<td><center><input type=radio value=&quot;Very Good&quot; name=Q01></td>
<td><center><input type=radio value=&quot;Good&quot; name=Q01></td>
<td><center><input type=radio value=&quot;Fair&quot; name=Q01></td>
<td><center><input type=radio value=&quot;Poor&quot; name=Q01></td>
</tr>
<tr>
<Td width=550><font face=&quot;arial&quot; size=&quot;-1&quot;><b>Payroll Specialist</b></td>
<td><center><input type=radio value=&quot;Excellent&quot; name=Q02></td>
<td><center><input type=radio value=&quot;Very Good&quot; name=Q02></td>
<td><center><input type=radio value=&quot;Good&quot; name=Q02></td>
<td><center><input type=radio value=&quot;Fair&quot; name=Q02></td>
<td><center><input type=radio value=&quot;Poor&quot; name=Q02></td>
</tr>
<tr>
<Td width=550><font face=&quot;arial&quot; size=&quot;-1&quot;><b>Operation Specialist</b></td>
<td><center><input type=radio value=&quot;Excellent&quot; name=Q03></td>
<td><center><input type=radio value=&quot;Very Good&quot; name=Q03></td>
<td><center><input type=radio value=&quot;Good&quot; name=Q03></td>
<td><center><input type=radio value=&quot;Fair&quot; name=Q03></td>
<td><center><input type=radio value=&quot;Poor&quot; name=Q03></td>
</tr>
<tr>
<Td width=550><font face=&quot;arial&quot; size=&quot;-1&quot;><b>Training / Compliance Specialist</b></td>
<td><center><input type=radio value=&quot;Excellent&quot; name=Q04></td>
<td><center><input type=radio value=&quot;Very Good&quot; name=Q04></td>
<td><center><input type=radio value=&quot;Good&quot; name=Q04></td>
<td><center><input type=radio value=&quot;Fair&quot; name=Q04></td>
<td><center><input type=radio value=&quot;Poor&quot; name=Q04></td>
</tr>
<tr>
<Td width=550><font face=&quot;arial&quot; size=&quot;-1&quot;><b>Employee Leasing Co. (EPIX)</b></td>
<td><center><input type=radio value=&quot;Excellent&quot; name=Q05></td>
<td><center><input type=radio value=&quot;Very Good&quot; name=Q05></td>
<td><center><input type=radio value=&quot;Good&quot; name=Q05></td>
<td><center><input type=radio value=&quot;Fair&quot; name=Q05></td>
<td><center><input type=radio value=&quot;Poor&quot; name=Q05></td>
</tr>

</table>

<input type=submit> Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048

mikewolf@tst-us.com
 
Great, this works perfectly!! Another star for you!

Thanks you sooo much!
 
I have 4 text boxes that I have to also chack for a value (they are not required, but if there is not a value for them when they user hits submit they should be advised that the entire form has not been completed.)

I've tried adding this functionality to this script and keep getting error messages, can someone help? here is the script:

function checkRequired(theForm){
//check ID first
if (theForm.Q15.value.length!=6)
{
alert( &quot;Please enter your six digit ID.&quot;);
theForm.Q15.focus();
return false;
}

//ID is OK so check for blank questions
var missMess = &quot;&quot;

for (x=1; x <= 14; x++){
if ( x < 10 ){
thisAnswer = eval(&quot;theForm.Q0&quot; + x )
}
else{
thisAnswer = eval(&quot;theForm.Q&quot; + x )
}
answered = false
for (y=0; y<thisAnswer.length; y++){
if (thisAnswer[y].checked){
answered = true
}
}
if (!answered){
missMess += &quot;\n-Question #&quot; + x
}
}

doAnyways = true
if (missMess != &quot;&quot;){
doAnyways = confirm(&quot;The entire survey has not been completed. Do you wish to submit anyway?&quot;)
}
return doAnyways;
}

 
The code I wrote only checks radio buttons with more than 1 in each radio group (Q01 to Q14). If some fields may not be radio group arrays try this (untested)...

function checkRequired(theForm){
//check ID first
if (theForm.Q15.value.length!=6)
{
alert( &quot;Please enter your six digit ID.&quot;);
theForm.Q15.focus();
return false;
}

//ID is OK so check for blank questions
var missMess = &quot;&quot;

for (x=1; x <= 14; x++){
if ( x < 10 ){
thisAnswer = eval(&quot;theForm.Q0&quot; + x )
}
else{
thisAnswer = eval(&quot;theForm.Q&quot; + x )
}
if (thisAnswer.length){
answered = false
for (y=0; y<thisAnswer.length; y++){
if (thisAnswer[y].checked){
answered = true
}
}
}
else{
if (thisAnswer.value == &quot;&quot;){
answered = false
}
else{
answered = true
}
}

if (!answered){
missMess += &quot;\n-Question #&quot; + x
}
}

doAnyways = true
if (missMess != &quot;&quot;){
doAnyways = confirm(&quot;The entire survey has not been completed. Do you wish to submit anyway?&quot;)
}
return doAnyways;
} 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