Wrox Programmer Forums
|
ASP Forms As of Oct 5, 2005, this forum is now locked. Please use "Classic ASP beginner" at http://p2p.wrox.com/forum.asp?FORUM_ID=54 or "Classic ASP Professional" http://p2p.wrox.com/forum.asp?FORUM_ID=56 instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP Forms section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 4th, 2005, 02:15 PM
Registered User
 
Join Date: May 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Javascript to Validate Asp Forms

I have developed a website and have included a form for new members to register. The website is coded in such a way that the Form is generated in ASP. This means that when I insert the JavaScript to validate the ASP form it won't work. Does anyone know how to validate it using Javascript without having to recreate the page so that the form isnt generated using ASP.

The ASP code is shown below:

<%
Content = "" 'Clear the Content string
QStr = Request.QueryString("login") 'Save the login querystring to QStr

if ucase(left(QStr,6))="CREATE" then
Title = "Registration"
else
Title = "Login"
end if

'The code below saves the contents the table must have in the variable Content
'The content depends on what's in the QueryString


'The form below is the form that I want to Validate using JavaScript. I want to validate all the usual items such as valid email, correct format age and salary and no fields left blank etc

if QStr="createnew" then
Content = Content & "<form name=frmCreate action=create.asp Method=post>"

Content = Content & "<tr><td ><br>Username: <input type=text name=txtUsername></td></tr>"

Content = Content & "<tr><td >Password: <input type=password name=txtPassword></td></tr>"

Content = Content & "<tr><td ><br>First name: <input type=text name=txtFirstName></td></tr>"

Content = Content & "<tr><td ><br>Surname: <input type=text name=txtLastName></td></tr>"

Content = Content & "<tr><td ><br>Address: <input type=text name=txtAddress></td></tr>"

Content = Content & "<tr><td ><br>County: <input type=text name=txtCounty></td></tr>"

Content = Content & "<tr><td ><br>Postcode: <input type=text name=txtPostcode></td></tr>"

Content = Content & "<tr><td ><br>Age: <input type=text name=txtAge></td></tr>"

Content = Content & "<tr><td ><br>Income: <input type=text name=txtAnnualIncome></td></tr>"

Content = Content & "<tr><td ><br>Gender: <input type=text name=txtGender></td></tr>"

Content = Content & "<tr><td ><br>E-mail Address: <input type=text name=txtEmail></td></tr>"

Content = Content & "<tr><td ><input type=submit name=submit value=Register <td></tr>"

Content = Content & "</form>"

%>

'The following is code is where the table is generated to show the registration form

<table border="1" cellspacing="5" width="350px">
    <%
    Response.Write("<tr><td valign=top align=center><strong>" & Title & "</strong></td></tr>")
    Response.Write(Content) ' Paste the contents in the table
    %>
  </table>


If anyone knows how it can be done I would really appreciate the help.

Thanks

Gareth

 
Old May 4th, 2005, 02:48 PM
Registered User
 
Join Date: Mar 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to dwithrow Send a message via Yahoo to dwithrow
Default


Gareth,

Do it the same way you're creating the form. Just add the javascript to the Content variable you're carrying.

Content = Content & " <script language='JavaScript'>function formSubmit() { var success = True ... yada yada ... return success }</script>"

Be sure when you define your form you do it like:
Content = Content & "<form name=frmCreate action=create.asp Method=post onsubmit='return formSubmit()'>"

HTH
Don
 
Old May 4th, 2005, 04:15 PM
Registered User
 
Join Date: May 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Don

Really Appreciate your help. This has been driving me crazy all day.

Thanks Again
Gareth

 
Old May 4th, 2005, 05:08 PM
Registered User
 
Join Date: May 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am still having trouble. the problem is that i'm not sure exactly how to enter the javascript code so that the valdation can execute properly. The code below is how I inserted the javascript. Any suggestions on what i am doing wrong. I am still a begginner so i am still stuggling to understand everything:

elseif QStr="createnew" then
Content = Content & "<script language=javascript>

function checkForm(form) {
    var errors = '';
    var numErrors = 0;

    if (!isValidEmail(form.txtEmail.value)) {
     errors += '- The email field is not valid.\\n';
     numErrors++;
    }


    if (form.txtFirstName.value=="") {
     errors += '- You must enter a forename.\\n';
     numErrors++;
    }

    if (form.txtFirstname.value=="") {
     errors += '- You must enter a First Name.\\n';
     numErrors++;
    }

    if (form.txtEmail.value=="") {
     errors += '- You must enter an email address.\\n';
     numErrors++;
    }

    if (form.txtAddress.value=="") {
     errors += '- You must enter an address.\\n';
     numErrors++;
    }

    if (numErrors) {
     errors = 'The form was not submitted due to the following problem' + ((numErrors > 1) ? 's' : '') + ':\\n' + errors + 'Pl
ease fix ' + ((numErrors > 1) ? 'these' : 'this') + ' problem' + ((numErrors > 1) ? 's' : '') + ' and resubmit the form.';
     alert(errors);
     return false;
    }
    return true;
   }</script>"
        Content = Content & "<form name=frmCreate action=create.asp Method=post onsubmit=return formSubmit()>"
        Content = Content & "<tr><td ><br>Username: <input type=text name=txtUsername></td></tr>"
        Content = Content & "<tr><td >Password: <input type=password name=txtPassword></td></tr>"
        Content = Content & "<tr><td ><br>First name: <input type=text name=txtFirstName></td></tr>"
        Content = Content & "<tr><td ><br>Surname: <input type=text name=txtLastName></td></tr>"
        Content = Content & "<tr><td ><br>Address: <input type=text name=txtAddress></td></tr>"
        Content = Content & "<tr><td ><br>County: <input type=text name=txtCounty></td></tr>"

        Content = Content & "<tr><td ><br>Postcode: <input type=text name=txtPostcode></td></tr>"
        Content = Content & "<tr><td ><br>Age: <input type=text name=txtAge></td></tr>"
        Content = Content & "<tr><td ><br>Income: <input type=text name=txtAnnualIncome></td></tr>"
        Content = Content & "<tr><td ><br>Gender: <input type=text name=txtGender></td></tr>"
        Content = Content & "<tr><td ><br>E-mail Address: <input type=text name=txtEmail></td></tr>"
        Content = Content & "<tr><td ><input type=submit name=formSubmit value=Register onsubmit=javascript:return ValidateForm(this)></td></tr>"
        Content = Content & "</form>"

Any help would be appreciated.

Thanks
Gareth






Similar Threads
Thread Thread Starter Forum Replies Last Post
Validate Radiobuttonlist using Javascript roseforever C# 1 November 6th, 2008 04:55 AM
Javascript to validate a textbox abhishekkashyap27 Javascript 9 May 6th, 2008 02:41 AM
Validate duplicate Entries using javascript sams ASP.NET 1.0 and 1.1 Professional 3 July 2nd, 2007 03:31 AM
hi help me to write javascript to validate textbox karthikc85 XML 1 October 12th, 2006 03:12 AM
validate date javascript lcsgeek Classic ASP Basics 3 October 3rd, 2003 04:07 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.