Click to See Complete Forum and Search --> : ASP over-riding Javascript?


dennic
09-09-2003, 09:16 PM
I have just created an ASP form to email script for a contact page. On the contact page within the HTML, i have also created a javascript that validates to see whether certain fields have been completed, only problem now is, when I call the asp file (in the submit button), the javascript is not working.

do i have to rewrite the script within the asp file?

thanks

Superfly1611
09-10-2003, 04:55 AM
I think i understand your problem in which case it couldn't be simpler.....

You mean that you need the javascript to execute before you submit the form.....

change your
<input type=submit>

to
<input type=button onclick="Validation();">

If you haven't already done so declare your javascript as a function in your <head></head> tags and add onto the end of it document.form.submit();


<Head>

<script language=javascript>
function Validation()
{
//your validation goes here

document.form.submit();
}
</script>

</Head>


From now on when you press that button it will run the validation function on your form objects and then providing they are all good it will submit the form to the page.

Is that what you meant?

dennic
09-10-2003, 05:21 AM
ive got all that dude, it was working right before i used the asp form to email script



here is the code from my contact page
<SCRIPT LANGUAGE="javascript">

<!--

function submitted() {
c=document.contact;
if(c.name.value==""){
alert("You must enter your name!");
c.name.focus();
return false;
} else if(c.email.value==""){
alert("You must enter your email address!");
c.email.focus();
return false;
}
}
//-->
</SCRIPT>

<form method="get" action="Email.asp" name="Contact">


//fields are in here


<INPUT type="submit" value="Send mail" name="contact" onClick="return submitted()">&nbsp;

Superfly1611
09-10-2003, 05:37 AM
ok - i'm not 100% on what is going on (/me is easilly confused :o )....

So does the script execute but with an error or does it not execute at all?

jrthor2
09-10-2003, 10:10 AM
I do it this way, and it works.

In your Form properties, put onSubmit="return function()", like this:

<form name="amaze_comments" action="amaze_comments_thanks.asp" method="post" onSubmit="return valid()">

Then you don't need the onClick property for your submit button.

Here is my Javascritp in my head section:

<script>
var re = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
var alpha = new RegExp("[a-zA-Z]");var digit = new RegExp("[0-9]");var truecount=0;
function valid(){
truecount=0;
if(document.amaze_comments.Name.value==""){alert("Name is a required field!");truecount=0;return false;} else {truecount++;}
mail=document.amaze_comments.Email.value;if(!re.test(mail)){alert("Email is not a valid e-mail format");truecount=0;return false;} else {truecount++;}
if(document.amaze_comments.amaze_comments.value==""){alert("Comments is a required field!");truecount=0;return false;} else {truecount++;}
if(truecount==3){return true;} else { truecount=0; } }
</script>

dennic
09-11-2003, 07:17 AM
still not working

i dont suppose you know how to do it within the asp code?

here is the script i have used


'Fields to collect the information

<%
strName = Request.QueryString("Name")
strCompanyName = Request.QueryString("CompanyName")
strEmail = Request.QueryString("Email")
strMobileNumber = Request.QueryString("MobileNumber")
strFaxNumber = Request.QueryString("FaxNumber")
strComments = Request.QueryString("Comments")
%>

'Tells the customer what they have sent

Name: <%= strName %>
Company Name: <%= strCompanyName %>
Email: <%= strEmail %>
Mobile Number: <%= strMobileNumber %>
Fax Number: <%= strFaxNumber %>
Comments: <%= strComments %>

'Sends to info@dennic.com.au

<%
Set objCDOMessage = Server.CreateObject("CDONTS.NewMail")
'Creates the object
objCDOMessage.From = "Customer"
objCDOMessage.To = "info@dennic.com.au"
objCDOMessage.Subject = "Customer Enquiry Form"
objCDOMessage.Body = "The following information is submitted from customer" & vbcrlf &_

"Name:" & strName & vbcrlf &_
"Company Name:" & strCompanyName & vbcrlf &_
"Email:" & strEmail & vbcrlf &_
"Mobile Number:" & strMobileNumber & vbcrlf &_
"Fax Number:" & strFaxNumber & vbcrlf &_
"Comments:" & strComments

'Send the message
objCDOMessage.Send
'Finsh things off
Set objCDOMessgae = Nothing
Response.redirect("Thanks.htm")
%>

TBor
09-11-2003, 08:53 AM
try adding "javascript:" to the beginning of your onClick call. It's possible that your page is set up to use VBScript as its default scripting language and doesn't recognize the javascript commands.

Your Submit tag should look like this:

<INPUT type="submit" value="Send mail" name="contact" onClick="javascript:return submitted()">

personally, what I do is not return anything to the submit call, but at the end of the validation function (Submitted() in your case) I call the Submit property of the form (Contact.submit)

Doing this in your code:

<SCRIPT LANGUAGE="javascript">
<!--
function submitted() {
c=document.contact;
if(c.name.value==""){
alert("You must enter your name!");
c.name.focus();
} else if(c.email.value==""){
alert("You must enter your email address!");
c.email.focus();
} else {
c.submit()
}
}
//-->
</SCRIPT>

Then you'd have to remove the word "return" from your onClick statement in the Submit button tag.

Hope this helps,
TBor

dennic
09-13-2003, 05:24 AM
still no joy!
dont know what the problem is, i dont suppose you know how to do it within the ASP script?

rdoekes
09-13-2003, 11:54 AM
First conceptionally:
There are two ways to do validation.
1. round trip to the server, and doing the validation server-side. All the coding is done in ASP.
2. the validation client-side, before you submit the results to the server for server-side processing. Since javascript is the most widely browser supported scripting language, javascript is the best tool for the job. The ASP does nothing more than sending the email and redirecting to the thank-you page.

and finally both;) Some users disable javascript. For them validation server side is the only option. However, client side scripting does not require round trip to the server, meaning less load on the server. Like killing two birds with one stone.

what does this mean codewise:
ad 1. round trip
Your form is
<form method="get" action="email.asp">
name<input name="name"><br>
email<input name="email><br>
<input type="submit">
</form>

email.asp will look like this
strName = Request.QueryString("name")
strEmail = Request.QueryString("email")

'-------validation section---------------------
strMessage = ""
If Len(strName) = 0 Then strMessage = strMessage & "name is required field<br>"
If Len(strEmail) = 0 Then strMessage = strMessage & "email is required field<br>"

'-----email sending if validation is correct----
if Len(strMessage) = 0 Then
Set objCDOMessage = Server.CreateObject("CDONTS.NewMail")
'Creates the object
objCDOMessage.From = "Customer"
objCDOMessage.To = "info@dennic.com.au"
objCDOMessage.Subject = "Customer Enquiry Form"
objCDOMessage.Body = "The following information is submitted from customer" & vbcrlf &_
"Name:" & strName & vbcrlf &_
"Email:" & strEmail & vbcrlf

'Send the message
objCDOMessage.Send
'Finsh things off
Set objCDOMessgae = Nothing

Response.redirect("Thanks.htm")
End If
%>
<form action="email.asp" method="get">
<% = strMessage%><br>
<input name="name" value="<% = strName%>"><br>
<input name="email" value="<% = strEmail%>"><br>
<input type="submit">
</form>

ad 2: javascript
the form

<script language="JavaScript">
function validateForm(oForm) {
if (oForm.name.value.length==0) {
alert("name is required field"); oForm.name.focus();
return false; }
if (oForm.email.value.length==0) {
alert("email is required field"); oForm.email.focus();
return false; }
}
</script>
<form onSubmit="javascript:return validateForm(this);" method="get"
action="email.asp" name="myForm">
name<input name="name"><br>
email<input name="email><br>
<input type="submit">
</form>

Email.asp is like the following
Set objCDOMessage = Server.CreateObject("CDONTS.NewMail")
......
Response.Redirect("Thanks.htm")

ad 3. both.
add both together.

The key to make the javascript work is the name parameter in the <form> tag. If there is no name="blablabla" specified, javascript will not execute properly.

Sorry for the long tutorial style reply.

-Rogier Doekes