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!

validation script function call

Status
Not open for further replies.

frogggg

Programmer
Jan 17, 2002
182
US
I have an html form, in the form tag, onSubmit="return isReady(this)". The function works through all the textboxes on the form, if they are completed, rewrites the label and textbox with the value of the textbox inside, if not, writes the textbox label in red and another textbox. Essentially, I'm redrawing the entire form in the function, with form tags and a submit button. The function works fine, and so does the submit button inside the form. I also had a counter inside each red draw, and if the counter is 0, return true;, if not show the form and the submit button. The problem is if on the second try the form is still not properly filled in, I want the function to resubmit to itself. Right now, I have in the form tag in the function, onSubmit="return isReady(this)". This does absolutely nothing!

Does anyone have any ideas?

if (counter > 0)
{
document.write('<h3><font color=&quot;#ff0000&quot;>The fields in red are required.</font></h3>');
document.write('<tr><td><INPUT type=&quot;submit&quot; value=&quot;Submit&quot; id=submit2 name=submit2></td></tr>');
document.write('</table></form>');
return false;
//reset counter
counter = 0;
}
else
{
return true;
}

If it draws the page, it should be returning false to itself too, no?

Thanks for any help.
 
froggg,

I would probably set a session variable on the first try. So when you come back to that page and the variable is equal to or past that threshold, have the function resubmit it self. I hope I understood your problem correctly.

Cheers,
fengshui_1998
 
I guess that post was pretty confusing.
1)Form:
<form name=&quot;frmProfile&quot; id=&quot;form1&quot; onSubmit=&quot;return isReady(this)&quot; method=&quot;get&quot; action=&quot;addUser.asp&quot;>
some textboxes and a submit button
</form>

2)Validation Script:
function isReady(form)
{
//put values of textboxes into variables for ease in comparison
var fName;
fName = form.txtfName.value;
etc.
var counter;
counter = 0;
document.write('<form name=&quot;frmProfile&quot; onSubmit=&quot;return isReady(this)&quot; method=&quot;get&quot; action=&quot;addUser.asp&quot;>');
document.write('<table style=&quot;WIDTH: 305px; HEIGHT: 875px&quot; cellSpacing=&quot;2&quot; cellPadding=&quot;2&quot; width=&quot;305&quot; align=&quot;center&quot; border=&quot;0&quot; id=&quot;TABLE1&quot;>');
if (fName == &quot;&quot;)
{
document.write('<tr><td><font color=&quot;#ff0000&quot;>First Name: </font></td>');
document.write('<td><INPUT type=&quot;text&quot; id=text1 name=txtfName></td></tr>');
counter = counter + 1;
}
else
{
document.write('<tr><td>First Name: </td>');
document.write('<td><INPUT type=&quot;text&quot; id=text1 name=txtfName value=' + fName + '></td></tr>');
}

etc. for each of the textboxes.

Then, when they are all written to the new form either in black or red,

if (counter > 0)
{
document.write('<h3><font color=&quot;#ff0000&quot;>The fields in red are required.</font></h3>');
document.write('<tr><td><INPUT type=&quot;submit&quot; value=&quot;Submit&quot; id=submit2 name=submit2></td></tr>');
document.write('</table></form>');
return false;
//reset counter
counter = 0;
}
else
{
return true;
}

If return true, then the first form submits, no problem.
If return false, then the second form display, also, no problem. The question is, how to get it to go through the entire function again on the second submit, to ensure that textboxes were filled in correctly the second time around.

I hope that makes more sense. Thanks for your help.
 

When you did document.write the script function were deleted.
This may work for you

<%@ Language=VBScript %>
<HTML>
<HEAD>
<META NAME=&quot;GENERATOR&quot; Content=&quot;Microsoft Visual Studio 6.0&quot;>
<script language=javascript name=&quot;submitvalid&quot;>

function isReady(form)
{
//put values of textboxes into variables for ease in comparison
var fName;




alert(document.form1.innerHTML )

fName = form.txtfName.value;
lName=form.txtlName.value
document.form1.innerHTML =&quot;&quot;

var counter;
counter = 0;

document.form1.innerHTML =document.form1.innerHTML + '<table style=&quot;WIDTH: 305px; HEIGHT: 875px&quot; cellSpacing=&quot;2&quot; cellPadding=&quot;2&quot; width=&quot;305&quot; align=&quot;center&quot; border=&quot;0&quot; id=&quot;TABLE1&quot;>'
if (fName == &quot;&quot;)
{
document.form1.innerHTML =document.form1.innerHTML + '<tr><td><font color=&quot;#ff0000&quot;>First Name: </font></td>'
document.form1.innerHTML =document.form1.innerHTML +'<td><INPUT type=&quot;text&quot; id=text1 name=txtfName></td></tr>'

counter = counter + 1;
}
else
{
document.form1.innerHTML =document.form1.innerHTML + '<tr><td>First Name: </td>'
document.form1.innerHTML =document.form1.innerHTML + '<td><INPUT type=&quot;text&quot; id=text1 name=txtfName value=' + fName + '></td></tr>'
}
if (lName == &quot;&quot;)
{
document.form1.innerHTML =document.form1.innerHTML + '<tr><td><font color=&quot;#ff0000&quot;>Last Name: </font></td>'
document.form1.innerHTML =document.form1.innerHTML + '<td><INPUT type=&quot;text&quot; id=text1 name=txtlName></td></tr>'


counter = counter + 1;
}
else
{
document.form1.innerHTML =document.form1.innerHTML + '<tr><td>Last Name: </td>'
document.form1.innerHTML =document.form1.innerHTML + '<td><INPUT type=&quot;text&quot; id=text1 name=txtlName value=' + lName + '></td></tr>'

}



if (counter > 0)
{
document.form1.innerHTML =document.form1.innerHTML + '<h3><font color=&quot;#ff0000&quot;>The fields in red are required.</font></h3>'
document.form1.innerHTML =document.form1.innerHTML + '<tr><td><INPUT type=&quot;submit&quot; value=&quot;Submit&quot; id=submit2 name=submit2></td></tr>'
document.form1.innerHTML =document.form1.innerHTML + '</table>'//</form>'
alert( document.form1.innerHTML )
return false;
//reset counter
counter = 0;

}
else
{
return true;

}

}
</script>
</HEAD>
<BODY>
<div id=formme>
<form name=&quot;frmProfile&quot; id=&quot;form1&quot; onSubmit=&quot;return isReady(this)&quot; method=&quot;get&quot; action=&quot;addUser.asp&quot;>
<input type=text name=&quot;txtfName&quot;>
<input type=text name=&quot;txtlName&quot;>
<input type=submit>

</form>
</div>

</BODY>
</HTML>
 
Thank you very much! I never knew about that .innerHTML .
A few questions.
1) In the script tag, what does name=submitvalid mean and do?
2) Why do you print the alert twice?, once even before there is anything in it?
Also, I assume the missing ; was unintended and there should be one after the parentheses.

Thanks again!
 
1) just a name for the script block (it is not necessary)
2) just for testing to see what the original value of the innerHTML was compare to the final value

; are not required
 
When are ; not required? Is this a function call?
 
; are not required in javascript.

You can remove them all and the code will still work
 
Just one second, please. Where in the innerHTML is the page being submitted back to itself if it was improperly completed again? I really appreciate your help. Sorry if I didn't understand the first time.
 
Uh oh,
alert(document.form1.innerHTML)
only displayed all the code I just typed in.
And, after I pressed OK, I had the same code I had before, but not in tabular form - as if it missed the line about the <table>. Can you help?
 
The code is the same as your code(code submitted above). I replaced the document.write with document.form1.innerHTML. The only difference it that you do not have to rewrite the form code(ie <form etc) since you are only replacing the internals of the form using the innerHTML method. The other thing I changed it that I moved the script block into the head section of the html page.

I did notice that I included <%@ Language=VBScript %>. I included that because I used an asp page and it is not necessary since you are using only HTML

The innerHTML with not display the form code below:
<form name=&quot;frmProfile&quot; id=&quot;form1&quot; onSubmit=&quot;return isReady(this)&quot; method=&quot;get&quot; action=&quot;addUser.asp&quot;>

it will only display the code between the start form tag and end form tag
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top