Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

jsp code in javascript if statements

Status
Not open for further replies.

oceandeep

IS-IT--Management
Jul 6, 2000
69
GR
Hi,

Can anybody here help me out of this:

<%
String mask = "11111";
%>

<script language="javascript">
re = /^(0|1){5}/;
alert("a <%=mask%>");
if(!re.test(<%=mask%>))
{
<%mask="11111"%>;
}
alert("b <%=mask%>");
</script>

But it seems that the jsp code <%mask="11111"%> will be excuted no mater the if-statement is true or not.
Can the code be writen like that, or if there are some other tricks I need to play around to let it work?
Your help will be highly appreciated.
Thanks!
 
You need to understand that the JSP code is server-side and it is completed before the page is delivered to the web browser. The Javascript is client-side and is run once the page has been delivered to the browser. As such there is no way you can influence the value of a JSP variable from Javascript (in the way you have tried).

You can modify the script by assigning the value of the JSP variable mask to a javascript variable... and you can update that variable... but this will only be updating the javascript (not the JSP) variable.

You are probably having problems with you page not compiling as well... since your code example looks like it's missing some basic syntax.

Here is a reworked example of a possible solution:
Code:
<%
   String mask = "11111";
%>
<script type="text/javascript">
re = /^(0|1){5}/;
myData = <%=mask%>;
alert("a" + myData);
if(!re.test(myData))
{
   myData="22222";
}
alert("b" + myData);
</script>

You could do this server-side in Java and not require Javascript at all if that was your goal. I don't have regexp sample code to post for this handy. Maybe someone else?

Cheers,
Jeff
 
Thanks a lot, Jeff! Yes, I confused the way how JSP and Javascript works. Seems that I have to go back to the old times to use "normal" java code to do the validation. ;) I cannot use regexp in java because my customer still using JDK 1.3...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top