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?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
TheCarver
  • 19,391
  • 25
  • 99
  • 149
  • 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`) – Shadow The GPT Wizard Sep 21 '11 at 08:34

1 Answers1

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)"">"
John Hartsock
  • 85,422
  • 23
  • 131
  • 146
  • 1
    In ASP Classic, you escape quote by doubling, i.e. `"
    "`.
    – Thom Smith Sep 20 '11 at 23:57
  • @Thom Smith Yes...I have become accustom to .Net – John Hartsock Sep 20 '11 at 23:59
  • 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 – TheCarver Sep 21 '11 at 00:11
  • @Martin G If you do not have a submit button then how are you submitting the form and triggering the submit event in javascript? – John Hartsock Sep 21 '11 at 00:12
  • 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 – TheCarver Sep 21 '11 at 00:18
  • I think we are going to need more detail such as an example page or seeing the html generated – John Hartsock Sep 21 '11 at 00:20
  • I think I actually have something wrong with my Javascript. Even if I take away the ASP value from the parenthesis, remove the validation for that value and just have an alert when submitted, it fails. It also fails if I add a submit button – TheCarver Sep 21 '11 at 00:30
  • Nope, I just can't see what's wrong. I have just added a new section to my question, which should be a basic form validation... but still doesn't fire! – TheCarver Sep 21 '11 at 00:53
  • It's annoyed me so much, I may just be happy with my ASP validation for this text box. If user enters text it shows error page, if user enters 0 or less, it will go to page 1 and if the y go over the total amount of pages, it goes to the last page automatically. This will do but thought it would be nice with an alert box too – TheCarver Sep 21 '11 at 01:18