PDA

Click to See Complete Forum and Search --> : client side form validation w/javascript on a ASP page


pnj
Oct 14th, 2000, 04:56 PM
howdy,
I want to verify that a user has entered numbers into a form using javascript before the page is sent back to the server.
any suggestion on how to do this?
I can do the asp page and the form validation page(one asp,one html w/javascript) but I can't combine the two.

I will post my code if you need.
thank you

noone
Oct 14th, 2000, 05:11 PM
Does this link cover what you need?:
http://www.builder.com/Programming/Javascript/ss09.html

pnj
Oct 14th, 2000, 05:43 PM
here is my code.my problem lies in incorporating javascript in an ASP page.
+++++++++++++++++++++++++++++++++
<%
Response.Write "<h3>$100,000 Club</h3>"
Response.Write "<form name=theclub action=page2.asp method=post>"
Response.Write "<input type=text name=""start""> Starting Salary<br>"
Response.Write "<input type=text name=""raise"">Yearly % of income raise<br>"
Response.Write "<input type=text name=""year"">Starting Year<br>"
Response.Write "<input type=submit value=""submit"" onClick=java()><input type=reset value=clear>"


Response.Write "<script language=javascript>"
Response.Write "<!--"
Response.Write "function java()"
Response.Write "{"
Response.Write "if document.theclub.start.value !="" "";"
Response.Write "location.href=page2.asp"
Response.Write "return true;"
Response.Write "else{"
Response.Write "alert(enter something)return false;"
Response.Write "}"
Response.Write "}//-->"
Response.Write "</script>"

%>

+++++++++++++++++++++++++++++++++++++++++


when the user fills in the form and clicks the submit button
I want to call the javascript function and verifie that they actually filled the form out.

right now I get this error :
Microsoft VBScript runtime error '800a000d'

Type mismatch: '[string: ""]'

/asp/page2.asp, line 16

thanks.

monte96
Oct 14th, 2000, 07:31 PM
For starters, you want to submit your form. Just calling the 2nd page will not pass any of your form data to it. Instead of the location.href you should do theclub.submit()

Not sure what the javascript equivilent to IsNumeric() is though...

pnj
Oct 14th, 2000, 09:23 PM
I'm not sure what you meen exactly.

I can get this to work (with slightly different syntax) if I don't use any javascripting. the form works and does what I want it to. but I don't know how to incorporate the javascript function w/ it.

thanks

monte96
Oct 14th, 2000, 11:09 PM
Actually, as I look at your code again, the script is executed on the click of the submit button so you don't need to put the location.href in there at all, return true will submit it and redirect to page2.asp

pnj
Oct 14th, 2000, 11:26 PM
I took out the part you said.
if the form is filled out completly then it works.(calls the correct page and sends the correct variable)
if I don't fill out the form the javascript should take over and give an alert.
but instead I get an error on the next page(the second page is still called up from the server)
________________________________
Type mismatch: '[string: ""]'

/asp/page2.asp, line 18
________________________________

what is strange is that,like i said, if the form is filled out it works.

would you like me to post the code again for you?

I am really lost w/ this stuff.

thanks again.

monte96
Oct 15th, 2000, 02:45 AM
Actually, if the error is on page2.asp, post the code from it...

pnj
Oct 15th, 2000, 03:18 AM
here is the code for both pages.
page 1:
+++++++++++++++++++++++++++++++++++++++++++
<%
Response.Write "<h3>$100,000 Club</h3>"
Response.Write "<form name=theclub action=page2.asp method=post>"
Response.Write "<input type=text name=""start""> Starting Salary<br>"
Response.Write "<input type=text name=""raise"">Yearly % of income raise<br>"
Response.Write "<input type=text name=""year"">Starting Year<br>"
Response.Write "<input type=submit value=""submit"" onClick=java()><input type=reset value=clear>"


Response.Write "<script language=javascript>"
Response.Write "<!--"
Response.Write "function java()"
Response.Write "{"
Response.Write "if (document.theclub.start.value !="" "");"
'Response.Write "location.href=page2.asp"
Response.Write "return true;"
Response.Write "else{"
Response.Write "alert(fill out the form)return false;"
Response.Write "}"
Response.Write "}//-->"
Response.Write "</script>"

%>

+++++++++++++++++++++
and page 2
+++++++++++++++++++++
<%
option explicit

dim start, raise, year

start= Request.Form ("start")'starting salary
raise = Request.Form ("raise")'raise amount per year
year = Request.Form ("year")'starting year


Response.Write "<table border=1 width=300><tr><td>Salary</td><td>Year</td></tr>"


do while start <= 100000
dim newYear
year = year + 1
start = start + ((raise/100)* start)
Response.Write "<tr><td>" & FormatCurrency(start,2) & " </td><td>" & year &"</td></tr>"
newYear=newYear + 1
loop

Response.Write "<tr><td colspan=""2"">" & newYear & " years until" & FormatCurrency(start,2) & "</td></tr>"
Response.Write "</table>"

Response.Write "<center>"& newYear & "</center>"
Response.Write start & " " & raise & " " & year
Response.Write"<br>"

%>
++++++++++++++++++++++++++++++++++++++++++++++++

thanks.

Oct 16th, 2000, 02:23 AM
Why not try something like this:


Response.Write "<script language=javascript>"
Response.Write "<!--"
Response.Write "function java()"
Response.Write "{"
Response.Write "if (document.theclub.start.value =="" "")"
Response.Write "{"
Response.Write "alert(""Fill in start please!"");"
Response.Write "document.theclub.start.focus()"
Response.Write "return false"
Response.Write "}"
Response.Write "if (document.theclub.raise.value =="" "")"
Response.Write "{"
Response.Write "alert(""Fill in raise please!"");"
Response.Write "document.theclub.raise.focus()"
Response.Write "return false"
Response.Write "}"
Response.Write "if (document.theclub.year.value =="" "")"
Response.Write "{"
Response.Write "alert(""Fill in year please!"");"
Response.Write "document.theclub.year.focus()"
Response.Write "return false"
Response.Write "}"
Response.Write "return true;"
Response.Write "}//-->"
Response.Write "</script>"


Of course this doesn't test if the value is numeric yet, since I don't really have a clue to that code in javascript either. I might get back to you on that.

Mark Sreeves
Oct 16th, 2000, 03:29 AM
here's a function to check that the contents are numeric


function check(contents) {
if (((contents / contents) != 1) && (contents != 0)) {alert('Please enter only a number into this text box')}
}




I it seems unnecessarily complicated writing your javascipt using response.write()

if server side variables have to be written within the javascript you have no choice, but this is just plain client-side stuff.