Click to See Complete Forum and Search --> : String validation


widiyantosg
10-18-2004, 11:03 PM
Hi,

I'm a newbie in javascript. I hope somebody will help me.
For example I have 2 textboxes and 1 button. I want to make a validation that if the 2 textboxes is empty when the user click the button then it will show an alert box. How to do that?

here is for the textboxes:
String description = request.getParameter("description");
String quantity = request.getParameter("quantity");

if (description == null || quantity == null){
%>
<script>
alert ('Please enter the necessary information')
history.go(-1);
</script>
<%
}
Wat's wrong with the above code? any sample code? Thanks

javaNoobie
10-18-2004, 11:17 PM
Try validating against empty strings instead of null values

fredmv
10-18-2004, 11:23 PM
That isn't JavaScript, but rather JSP. Moving to the Java forum; you'll get more help over there. Please post in the correct forum next time.

widiyantosg
10-18-2004, 11:52 PM
Still can not.... any ideas?

ray326
10-19-2004, 05:39 PM
Being the MVC Nazi that I am, I'd say your MAIN problem is that you're processing that form with a JSP instead of a servlet.

Javanoobie is right but for tests like that I prefer:

if (description != null && !description.equals("")
&& quantity != null && !quantity.equals("")) { %>
<p>Do HTML for good stuff</p>
<% } else { %>
<p>Do HTML for bad stuff</p>
<% } %>

widiyantosg
10-20-2004, 04:12 AM
:D Hi, thanks.. but I prefer doing the validation by using javascript since it is faster but I don't know how... :D any sample code?

<form name="test" method="post" action="request_action1.jsp?action=addrequest">
<input name="description" type="text" size="75">
function validateform()
{
var myForm = document.test;

if (myForm.description.length == 0)
{
alert("Please enter the item description");
history.go(-1);
}
}

but the alert can not be shown... can help me resolve the problem?

javaNoobie
10-20-2004, 04:48 AM
myForm.description.value.length

Note: There are plenty of validation codes if you do a search within the Javascript forum.