Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within JSP
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
JSP
Field validation with directive
Roberto Alves Pereira
Greenhorn
Posts: 13
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
cabecalho.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> <script language = "javascript" src = "Exercicio19.js"></script> </head> <body>
Exercicio.jsp
<%@ include file = "cabecalho.html" %> <%! static int iContador = 0; %> <% iContador++; %> <form name = "formulario" id = "formulario" method = "get"> <%= iContador %> <br /> <label for = "inicio">inicio: </label> <input type = "text" name = "inicio" id = "inicio" /> <br /> <label for = "fim">fim: </label> <input type = "text" name = "fim" id = "fim" /> <br /> <input type = "button" value = "Calcular" onclick = "verifica();"/> <br /> <input type = "text" id = "soma"/> </form> <%@ include file= "rodape.html" %>
Exercicio19.js
var req; function verifica() { var num1 = parseInt(document.getElementById("inicio").value); var num2 = parseInt(document.getElementById("fim").value); var bol = true; if(num1 < 0 || num1 >50) { bol = false; alert("Placing inicio value of 0 through 20."); document.formulario.inicio.focus(); } else if(num2 < 0 || num2 > 50) { bol = false; alert("Putting fim value between 0 to 20."); document.formulario.fim.focus(); } else if(isNaN(num1)) { bol = false; alert("This is not a number"); document.formulario.inicio.focus(); } else if(isNaN(num2)) { bol = false; alert("This is not a number"); document.formulario.fim.focus(); } else { if(num1 > num2) { bol = false; alert("inicio is greater than fim."); document.formulario.inicio.focus(); } else if(num1 == num2) { bol = false; alert("inicio equals fim."); document.formulario.fim.focus(); } } if(bol) { somatorio(); } } function somatorio() { var inicio = document.getElementById("inicio"); var fim = document.getElementById("fim"); var url = "Exercicio19.jsp?inicioo=" + escape(inicio.value) + "&fimm=" + escape(fim.value); if (window.XMLHttpRequest) { // requisição assincrona req = new XMLHttpRequest(); } else if (window.ActiveXObject) { req = new ActiveXObject("Microsoft.XMLHTTP"); } req.open("GET", url, true); // true requisição assincrona, se false ela é sincrona. req.onreadystatechange = callback; // quando pronto muda o estado req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); req.send(null); } function callback() { if (req.readyState == 4) { if (req.status == 200) { var soma = document.getElementById("soma"); soma.value = req.responseText; } else alert("Status: " + req.statusText); } clear(); } function clear() { var inicio = document.getElementById("inicio"); var fim = document.getElementById("fim"); inicio.value = ""; fim.value = ""; }
Exercicio19.jsp
<%! public boolean validaNumero(int numero) // Valid number.. { if(numero < 0 || numero > 21) return false; else return true; } %> <% String inicio = request.getParameter("inicioo"); String fim = request.getParameter("fimm"); boolean bol = true; if(inicio == null || inicio == "") { out.println("This is not a number"); bol = false; } else if(fim == null || fim == "") { bol = false; out.println("This is not a number."); } else { int auxInicio = Integer.parseInt(inicio); int auxFim = Integer.parseInt(fim); if(auxInicio > auxFim) { bol = false; out.println("Inicio is greater than fim"); } if(auxInicio == auxFim) { bol = false; out.println("Inicio equals fim"); } if(validaNumero(auxInicio)) { bol = false; out.println("Value invalid"); } if(validaNumero(auxFim)) { bol = false; out.println("Value invalid"); } if(bol) { int soma = 0; for(int cont = auxInicio; cont < auxFim; cont++) { soma += cont + auxFim; } out.println(soma); } } %>
rodape.html
</body> </html>
project (eclipse)
https://www.dropbox.com/s/9nctwcu0y2aoacr/Exercicio19.rar
Server validation always invalid by the Directive because?
Note: This is an exercise in college
Mansukhdeep Thind
Ranch Hand
Posts: 1164
I like...
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Welcome to the ranch Roberto. :) I could not understand what your doubt is. Can you explain in some more detail as to exactly what is the problem that you are facing?
~ Mansukh
Roberto Alves Pereira
Greenhorn
Posts: 13
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
1 - Run the application
2 - Enter number 1 (beginning)
3 - Digita number 2 (end)
Server validation if number1 <0 | | number2> 20 return false
The program never returns true
The program comes in is always returns false
Sorry for english mistakes, I am beginner
Mansukhdeep Thind
Ranch Hand
Posts: 1164
I like...
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Don't apologize. I can tell by your name that you are of Hispanic descent. :)
~ Mansukh
Mansukhdeep Thind
Ranch Hand
Posts: 1164
I like...
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Your method is :
<%! public boolean validaNumero(int numero) { if(numero < 0 || numero > 21) return false; else return true; } %>
I debugged and saw that it is written to return false if the numbers are either <0 or > 21. How can you expect it to work otherwise? :)
~ Mansukh
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Tomcat: No Java compiler was found
help me about logic:iterate and javascript
Large Numbers
wrapping a function
trying to insert values into select using javascript
More...