View Full Version : Validate Form before submit
ScottInTexas
06-01-2003, 08:22 PM
I want to validate info on this form before submitting it but my javascript isn't even getting called. My form is set up like this;
<form Name="infoForm" action="sendForm.asp" method="post">
.
.
.
<tr><td class="fldName">Comments</td><td class="field"><input type="text" name="cmnts"></td></tr>
</table>
<input type="button" value="Send" onClick="SubmitForm()">
</form>
And the javascript is this;
function mailMe(){
document.infoForm.submit();
}
function SubmitForm() {
if (EmailOK() && FirstNameOK() && LastNameOK() && StreetOK() && CityOK() && StateOK() && ZipOK())
mailMe();
}
The ASP is running and it is putting the data in the database as desired, however, it will also put a blank record in the database because it doesn't make sure the required fields are checked.
Thanks for your help.
I'm wondering why it is posted here. Validating it before 'submitting' it --> must be clientsided. I don't do clientsided because:
http://www.codingforums.com/showthread.php?s=&threadid=20094&highlight=validate+javascript
Why not post it i the javascript forum?
Anyway, if you'd deside to validate the form serversided, you could use a "for each element in request.form" loop to loop through the complete formscolletion and check all the fields.
ScottInTexas
06-02-2003, 02:17 PM
So if I do ALL server side checking and I want the user to add just the missing email, for instance, how do I redisplay the page with just the email space blank? Everytime the page comes up the form gets reinitialized.
I guess you're looking for a multipurpose page. The page is posted to itself and the form gets validated. If validation is passed, it gets processed. If validation is not passed, it is displayed again with the incorrect values (empty or incorrect) marked.
maybe check out this thread
http://www.codingforums.com/showthread.php?s=&threadid=20558&highlight=multipurpose+form
if you need more info on this, just let us know
Roy Sinclair
06-02-2003, 09:31 PM
Always, always, ALWAYS validate all the information a user posts on the server side. You can also use javascript to validate it on the client side for user convenience but since users can disable javascript or save and edit your html you will still need to validate their submissions in your server code anyway.
Here's a simple example of the kind of ASP page you need:
<% option explicit %>
<%
Dim formResult
Dim formError
formError = ""
formResult = ""
if Len(Request.Forms("formResult")) > 0 then
formResult = LCase(Request.Forms("formResult"))
if formResult <> "valid" then
formError = formError & "<b>You must enter the word VALID</b>"
end if
' Repeat previous if for every form field to be checked
end if
if Len(formError) < 1 then
' Write your code for handling a valid form here
'response.redirect("acceptedyourinput.asp) ' Optional redirect to the next page if any
end if
%>
<html>
<head>
<title>Sample form page</title>
</head>
<body>
Enter the word "valid":
<form>
<input type="text" size="5" name="formResult" value="<%=formResult%>" />
<input type="submit" />
</form>
<br />
<%=formError%>
</body>
</html>
I didn't actually test this so...
ScottInTexas
06-03-2003, 04:23 PM
Thanks very much for the responses. My final question would then be;
Can the response.write or response.redirect be directed to a specific location on a page? An iframe is the main focus of the website. Everything is displayed inside the iframe. Outside the iframe are the menus, the header and the footer.
So when I validate the form I would like to keep everything inside the iframe. I don't have any problem validating in ASP, just can't figure out how I return from the validation either an Invalid Form message or the page from which it was called without reloading the whole site?
Roy Sinclair
06-03-2003, 04:39 PM
Think of an IFRAME as another whole browser window that just happens to be embedded within a different page. The page in that iframe can be submitted and replaced without affecting anything else on the page it's embedded within.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.