0

Is it possible to call a JS function, with a value from an ASP script?

I thought it was possible, so I tried the following method but I can't seem to get it to run. As you can see, I have an ASP variable "totalPages" in the JS call.

Response.Write "<form name=""pageSkip"" onsubmit=""return validatePageSkip("&totalPages&")"">"

I then have the JS function, which is:

function validatePageSkip(totalPages)
{
     if (document.pageSkip.pn.value.length > totalPages)
     {
         alert("Please enter a page number within range!");
         document.pageSkip.pn.focus();
         return(false);
     }
     document.pageSkip.submit();
}
  1. Is this possible?
  2. If yes, what am I doing wrong?

UPDATE

It won't even work just using this, when I submit with an empty text box:

Response.Write "<form name=""pageSkip"" id=""pageSkip"" onsubmit=""return validatePageSkip()"" method=""post"" action=""?cpr="&pageRange&"#main"">"
Response.Write "<input type=""text"" name=""cpn"" id=""cpn"" spellcheck=""false"" autocomplete=""off"" size=""3"" class=""pageSkipBox"" value="""&currentPage&""">"
Response.Write "</form>"

With this simple JS function:

function validatePageSkip()
{
if (document.pageSkip.cpn.value.length < 1)
{
alert("Test!");
document.pageSkip.cpn.focus();
return(false);
}
document.pageSkip.submit();
}

I really don't know what is wrong. The form submits perfectly but it just won't call the function.

Can you spot anything?

1
  • Where is validatePageSkip written? How? Maybe you don't have it in the proper place. Anyway, add more alert commands into that function, one in the very beginning (before the if) Commented Sep 21, 2011 at 8:34

1 Answer 1

3

Yes it is possible. but you need to make sure you escape your quotes correctly

Response.Write "<form name=""pageSkip"" onsubmit=""return validatePageSkip("&totalPages&")"">"

Personally I would prefer doing something like this in the head

<head>
   <script type="text\javascript">
     var totalpages = <%=totalPages%>;
   </script>
</head>

Then

Response.Write "<form name=""pageSkip"" onsubmit=""return validatePageSkip(totalpages)"">"
Sign up to request clarification or add additional context in comments.

9 Comments

In ASP Classic, you escape quote by doubling, i.e. "<form name=""pageSkip"" onsubmit=""return validatePageSkip(" & totalPages & ")"">".
@Thom Smith Yes...I have become accustom to .Net
My double quotes were in-fact in place from the start, I wrote the form tag off the top of my head and ended up sticking Response.Write at the start in a last minute thing... This still isn't happening for me. I haven't got a submit button, is it possible that is causing it not to work? It's part of my recordset paging, first, prev, textbox, next, last, so there is no button
@Martin G If you do not have a submit button then how are you submitting the form and triggering the submit event in javascript?
The form depends on the user clicking return to submit it. The actually functionality of the form works perfectly, apart from validating the input against the ASP value
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.