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 October 14th, 2003, 07:10 AM
Registered User
 
Join Date: Oct 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default JavaScript validation with ASP

Hi folks,

Please excuse me folks but I new to all this programming lack so I'm sure you lovely people will be able to help me without even breaking sweat!

I have a form, that has some JavaScript validating the email address and then a submit button that goes to an ASP where the information from the form is stuck in an email and sent off. The problem is I can't get both working at the same time... the check works but the form is sent off regardless of whether the the email addresses match or have been filed in.

The code:

On the submit button
<input name="Submit" type="submit" id="Submit" onClick="CheckEmail()" value="Submit">


The script
// (C) 2000 www.CodeLifter.com
// http://www.codelifter.com
// Free for all users, but leave in this header

var good;
function checkEmailAddress(field) {

// Note: The next expression must be all on one line...
// allow no spaces, linefeeds, or carriage returns!
var goodEmail = field.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.go v)|(\.org)|(\..{2,2}))$)\b/gi);

if (goodEmail){
   good = true
} else {
   alert('Please enter a valid e-mail address.')
   field.focus()
   field.select()
   good = false
   }
}

function CheckEmail(){

   good = false
   checkEmailAddress(document.TinnitusLaunch.email)
   if ((document.TinnitusLaunch.email.value ==
        document.TinnitusLaunch.emailx.value)&&(good)){

      window.location='http://www.rnid.org.uk/html/information/tinnitus/launch/invite_thanks.asp'
   }
   if ((document.TinnitusLaunch.email.value !=
          document.TinnitusLaunch.emailx.value)&&(good)){
          alert('Both e-mail address entries must match.')
   }
}

The asp bit on the next page
 <%
        Dim strEmailAddress

        strEmailAddress = Request.Form("email")

    Set MyCDONTSMail = CreateObject("CDONTS.NewMail")
    MyCDONTSMail.From = strEmailAddress
    MyCDONTSMail.To= "[email protected]"
       MyCDONTSMail.Subject="Understanding tinnitus - confirmation of attendance"
    MyBody = "The following information has been submitted using the online form:"
    MyBody = MyBody & vbCrLf & "E-mail: " & request.form("email")
    MyBody = MyBody & vbCrLf & "Organisation: " & request.form("organisation")
    MyBody = MyBody & vbCrLf & "Password: " & request.form("password1") & " - " & request.form("password2") & " - " & request.form("password3")

    MyCDONTSMail.Body= MyBody
    MyCDONTSMail.Send
    set MyCDONTSMail=nothing
%>

Any ideas/help greatly appreciated
 
Old October 14th, 2003, 07:14 AM
Authorized User
 
Join Date: Sep 2003
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to balakumar1000
Default

<input name="Submit" type="submit" id="Submit" onClick="CheckEmail()" value="Submit">

dont put the value="Submit" in Input element. Write Code in CheckEMain() function that will submit the form if the email is valid. (document.<formname>.action = "post"
document.<formname>.submit())

This will do

Balakumar V.

 
Old October 20th, 2003, 08:31 AM
Registered User
 
Join Date: Oct 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Sorry been trying that and other things and still can't get it to work. Probably me sticking the code in the wrong place but as I said I'm new to all this coding and am struggling to get the JavaScript to valid the form and then the asp to put the info into an email. Can get one working independently of the other but not both at the same time!
 
Old October 22nd, 2003, 05:44 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 112
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You probably figured it out by now... But here is an example of what I use :) If the email is not deemed valid, the form is not posted.

<script language="JavaScript"><!--
function Form_Validator(theForm) {
    var regex = /^[\w\.-]+@[\w\.-]+\.[a-zA-Z]{2,3}$/
    if (!regex.test(theForm.Email.value)) {
        alert("Please enter a valid email address for the \"Email\" field.");
        theForm.Email.focus();
        return (false);
    }
        return (true);
}
//-->
</script>

// some more html here ...

<form method="POST" onsubmit="return Validate(this)" action="mypage.asp">
  <input type="text" name="Email">
  <input type="submit" value="Submit">
</form>

You are missing the return statement.


 
Old October 23rd, 2003, 04:04 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 479
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via MSN to surendran Send a message via Yahoo to surendran
Default

hi Davids
this code is very useful for you
first copy this code and add a name contactus.asp
'''''''''''''contactus.asp''''''''''''''''''''''
<html>

<head>
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<script language="JavaScript">
<!--
function onSubmit1()
{
if (document.form1.txtContactName.value=="")
{
    alert("Please Enter the Contact Name ")
    document.form1.txtContactName.focus()
    return;
}
 if ((document.form1.txtEmail.value == "")&&(document.form1.txtFax.value == "")&&(document.form1.txtTel.value == ""))
  {
    alert("Please enter a value for E mail / Phone / Fax");
     document.form1.txtTel.focus();
    return;
  }



   if (document.form1.txtEmail.value!= "")
  {
 var mail1=document.form1.txtEmail.value;
  var i1 = mail1.indexOf('@');
   var i2 = mail1.lastIndexOf('@');
   var i3 = mail1.indexOf('.');
  if (mail1.length<=5)
  {
  alert("Your E-mail address is too small");
   document.form1.txtEmail.focus();
  return;
   }

    if(i1==-1)
 {
  alert(" E-mail address must have a '@' ");
   document.form1.txtEmail.focus();
 return;
  }

   if (i3+1==mail1.length)
  {
 alert(" E-mail address cannot contain '.' in end");
  document.form1.txtEmail.focus();
  return;
 }

  if(i1!=i2)
{
 alert(" E-mail address cannot contain two '@' signs");
  document.form1.txtEmail.focus();
 return;
  }
}


if (document.form1.txtEmail.value!= "")
{
     var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw xyzƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎà ÃÃ‘Ã’Ã“Ã”Ã•Ã–Ã˜Ã™ÃšÃ›ÃœÃÃžÃŸÃ Ã¡Ã¢Ã£Ã¤Ã¥Ã¦Ã§Ã¨Ã ©ÃªÃ«Ã¬Ã­Ã®Ã¯Ã°Ã±Ã²Ã³Ã´ÃµÃ¶Ã¸Ã¹ÃºÃ»Ã¼Ã½Ã¾012345678 9-_.@";
    var checkStr = document.form1.txtEmail.value;
    var allValid = true;
    for (i = 0;i<checkStr.length;i++)
    {
        ch = checkStr.charAt(i);
        for (j = 0; j < checkOK.length; j++)
        if (ch == checkOK.charAt(j))
            break;
        if (j == checkOK.length)
        {
            allValid = false;
            break;
        }
    }
    if (!allValid)
    {
        alert("Please enter only letter, digit and \"@\" characters in the \"E-mail\" field.");
        document.form1.txtEmail.focus();
        return;
    }

}
if(document.form1.txtEnquire.value=="")
{
    alert("Please Enter the Message");
    document.form1.txtEnquire.focus ();
    return;
}
else
{
document.form1.mode.value="mailit"
document.form1 .action ="contactus.asp"
document.form1 .submit ();
}
}
//-->
</script>
</head>
  <%
    mode=Request("mode")
%>
<body topmargin="0" leftmargin="0" >
<%if mode<>"mailit" then%>
   <form method="post" name="form1">
  <table border="0" width="100%" height="" cellpadding="0" cellspacing="0" bordercolor=red>
  <tr>
    <td width="4%" height="15">&nbsp;</td>
        <td width="16%" height="15" colspan="3" align="center" >
        <table width="100%" border=0 bordercolor=red>
        <td width="33%"></td>
        <td width="33%" align=center><b>Contact us</b> </td>
        <td width="33%" align=right><a href="default.htm"><img border="0" src="images/home.gif" width="44" height="21"></a> </td>
        </table>
        </td>

      <td width="4%" height="15">&nbsp;</td>
    </tr>
     <tr>
      <td width="4%" height="15">&nbsp;</td>
      <td width="16%" height="15" colspan="3" align="center" >&nbsp;</td>
      <td width="4%" height="15">&nbsp;</td>
    </tr>
        <tr>
      <td width="4%" height="15">&nbsp;</td>
      <td width="16%" height="15" colspan="3"></td>
      <td width="4%" height="15">&nbsp;</td>
    </tr>
    <tr>
     <td width="4%" >&nbsp;</td>
      <td width="26%" >Contact Name</td>
      <td width="2%" >:</td>
      <td width="63%" ><input size=35 name="txtContactName" style="border: 1 solid #008000">&nbsp;*</td>
      <td width="4%"></td>
    </tr>
    <tr>
     <td width="4%" >&nbsp;</td>
      <td width="26%" >Company</td>
       <td width="2%" >:</td>
      <td width="63%"><input size=35 name="txtCompany" style="border: 1 solid #008000"></td>
      <td width="4%" >&nbsp;</td>
    </tr>
    <tr>
    <td width="4%" >&nbsp;</td>
      <td width="26%" >Address </td>
     <td width="2%" >:</td>
      <td width="63%" ><input size=35 name="txtAddress1" style="border: 1 solid #008000"></td>
      <td width="4%" >&nbsp;</td>
    </tr>
    <tr>
    <td width="4%" >&nbsp;</td>
      <td width="26%" height="27" >City</td>
      <td width="2%" >:</td>
      <td width="63%" height="27"><input size=35 name="txtAddress2" style="border: 1 solid #008000"></td>
      <td width="4%" height="27">&nbsp;</td>
    </tr>
    <tr>
    <td width="4%" >&nbsp;</td>
      <td width="26%" >County</td>
       <td width="2%" >:</td>
      <td width="63%" ><input size=35 name="txtAddress3" style="border: 1 solid #008000"></td>
      <td width="4%" >&nbsp;</td>
    </tr>
    <tr>
    <td width="4%" >&nbsp;</td>
      <td width="26%" >Post Code&nbsp;&nbsp;&nbsp;
            </td>
     <td width="2%" >:</td>
          <td width="63%" ><input size=20 name="txtPostCode" style="border: 1 solid #008000"></td>
      <td width="4%" >&nbsp;</td>
    </tr>
    <tr>
    <td width="4%" >&nbsp;</td>
      <td width="26%" >Tel</td>
       <td width="2%" >:</td>
      <td width="63%" ><input size=20 name="txtTel" style="border: 1 solid #008000"></td>
      <td width="4%" >&nbsp;</td>
    </tr>
    <tr>
    <td width="4%" >&nbsp;</td>
      <td width="26%" >Fax</td>
       <td width="2%" >:</td>
      <td width="63%" ><input size=20 name="txtFax" style="border: 1 solid #008000"></td>
      <td width="4%" >&nbsp;</td>
    </tr>
    <tr>
    <td width="4%" >&nbsp;</td>
      <td width="26%" >E mail</td>
      <td width="2%" >:</td>
     <td width="63%" ><input size=35 name="txtEmail" style="border: 1 solid #008000"></td>
      <td width="4%" >&nbsp;</td>
    </tr>
    <tr>
     <td width="4%" >&nbsp;</td>
      <td width="26%" >Enquiry Details</td>
      <td width="2%" >:</td>
      <td width="63%" valign="top"><TEXTAREA name=txtEnquire rows="6" cols="30" style="border: 1 solid #008000"></TEXTAREA>&nbsp;*</td>
      <td width="4%" >&nbsp;</td>
    </tr>
     <tr>
     <td width="4%" height="" >&nbsp;</td>
      <td width="26%" height="" >&nbsp;</td>
       <td width="2%" height="">&nbsp;</td>
      <td width="63%" height="">&nbsp;</td>
      <td width="4%" height="">&nbsp;</td>
    </tr>
    <tr>
      <td width="63%" height="" colspan="5" align="center" colspan="2">
     <a href="javascript:onSubmit1()"><IMG height=23 src="images/btnsubmit.GIF" width=78 border=0></a>&nbsp; </td>
    </tr>

    <tr>
        <td width="4%" height="" >&nbsp;</td>
         <td width="16%" height="" >&nbsp;</td>
          <td width="12%" height="">&nbsp;</td>
         <td width="63%" height="">&nbsp;</td>
         <td width="4%" height="">&nbsp;</td>
       </tr>
    <tr>
        <td width="4%" height="" ></td>
         <td width="91%" height="" colspan="3"></td>
         <td width="4%" height=""></td>
       </tr>

       <tr>
    <td width="4%" height></td>
    <td width="92%" height colspan=3></td>
    <td width="4%" height></td>
  </tr>
           <tr>
        <td width="4%" height="" >&nbsp;</td>
         <td width="16%" height="" >&nbsp;</td>
          <td width="12%" height="">&nbsp;</td>
         <td width="63%" height="">&nbsp;</td>
         <td width="4%" height="">&nbsp;</td>
       </tr>
  </table>
 <input class="smalltxt5" name="mode" type="hidden">
 </form>
 <%end if%>
  <%
if mode="mailit" then
    ContactName=Request("txtContactName")
    Company=Request("txtCompany")
    Address1=Request("txtAddress1")
    Address2=Request("txtAddress2")
    Address3=Request("txtAddress3")
    PostCode=Request("txtPostCode")
    Tel=Request("txtTel")
    Fax=Request("txtFax")
    Email=Request("txtEmail")
    Enquire=Request("txtEnquire")
    path=Request("HTTP_REFERER")
str=""
str=str+"<table border='0'bordercolor='red' align='center' width='90%' height='' cellpadding='0' cellspacing='0'>"
str=str+"<tr>"
str=str+"<td width='21%' align='center' colspan='3' height='23' ><b>Contact Us</b></td>"
str=str+"<td width='4%' height='23'></td>"
str=str+"</tr>"
str=str+"<tr>"
str=str+"<td width='21%' height='23'>Contact Name</td>"
str=str+"<td width='12%' height='23'>:</td>"
str=str+"<td width='63%' height='23'>"&ContactName&"</td>"
str=str+"<td width='4%' height='23'></td>"
str=str+"</tr>"

str=str+"<tr>"
str=str+"<td width='21%' height='23'>Company</td>"
str=str+"<td width='12%' height='23'>:</td>"
str=str+"<td width='63%' height='23'>"&Company&"</td>"
str=str+"<td width='4%' height='23'></td>"
str=str+"</tr>"
str=str+"<tr>"
str=str+"<td width='21%' height='23'>Address</td>"
str=str+"<td width='12%' height='23'>:</td>"
str=str+"<td width='63%' height='23'>"&Address1&"</td>"
str=str+"<td width='4%' height='23'></td>"
str=str+"</tr>"
str=str+"<tr>"
str=str+"<td width='21%' height='27'>City</td>"
str=str+"<td width='12%' height='23'>:</td>"
str=str+"<td width='63%' height='27'>"&Address2&"</td>"
str=str+"<td width='4%' height='27'></td>"
str=str+"</tr>"
str=str+"<tr>"
str=str+"<td width='21%' height='23'>County</td>"
str=str+"<td width='12%' height='23'>:</td>"
str=str+"<td width='63%' height='23'>"&Address3&"</td>"
str=str+"<td width='4%' height='23'></td>"
str=str+"</tr>"
str=str+"<tr>"
str=str+"<td width='21%' height='23'>Post Code&nbsp;&nbsp;&nbsp; </td>"
str=str+"<td width='12%' height='23'>:</td>"
str=str+"<td width='63%' height='23'>"&PostCode&"</td>"
str=str+"<td width='4%' height='23'></td>"
str=str+"</tr>"
str=str+"<tr>"
str=str+"<td width='21%' height='23'>Tel</td>"
str=str+"<td width='12%' height='23'>:</td>"
str=str+"<td width='63%' height='23'>"&Tel&"</td>"
str=str+"<td width='4%' height='23'></td>"
str=str+"</tr>"
str=str+"<tr>"
str=str+"<td width='21%' height='23'>Fax</td>"
str=str+"<td width='12%' height='23'>:</td>"
str=str+"<td width='63%' height='23'>"&Fax&"</td>"
str=str+"<td width='4%' height='23'></td>"
str=str+"</tr>"
str=str+"<tr>"
str=str+"<td width='21%' height='23'>E mail</td>"
str=str+"<td width='12%' height='23'>:</td>"
str=str+"<td width='63%' height='23'>"&Email&"</td>"
str=str+"<td width='4%' height='23'></td>"
str=str+"</tr>"
str=str+"<tr>"
str=str+"<td width='21%' height='23'>Enquiry Details</td>"
str=str+"<td width='12%' height='23'>:</td>"
str=str+"<td width='63%' height='23'>"&Enquire&"</td>"
str=str+"<td width='4%' height='23'></td>"
str=str+"</tr></table>"



    set mail_Obg=createObject("CDONTS.newmail")
    if Email="" then
    mail_Obg.From="[email protected]"
    else
    mail_Obg.From=Email
    end if
    mail_Obg.To ="[email protected]"
    strtobcc="[email protected];[email protected]"
    mail_Obg.Subject = "Enquiry"
    mail_Obg.BodyFormat=0
    mail_Obg.MailFormat=0
    mail_Obg.Body =str
    mail_Obg.Bcc = strtobcc
    mail_Obg.Importance=2
    mail_Obg.Send
    'Response.Write str
    set mail_Obg=nothing
    'msg="Sorry,unable to send mail!"
Response.Write str
end if
%>
</body>

</html>

surendran
 
Old December 6th, 2004, 12:49 PM
Registered User
 
Join Date: Dec 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Very Smart of you UNCLE






Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript datetime validation Hannibal Javascript 1 December 5th, 2007 06:39 AM
Javascript validation error in asp.net shoakat ASP.NET 2.0 Professional 0 August 15th, 2007 11:53 AM
using javascript for server control validation swagatika ASP.NET 1.0 and 1.1 Basics 3 December 20th, 2005 08:38 AM
Javascript validation for apostrophe crmpicco Javascript How-To 2 March 2nd, 2005 06:57 AM
JAVASCRIPT input validation ! Please help ! sammes Javascript 2 July 19th, 2004 10:47 AM





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