This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSP and the fly likes Validate input Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Validate input" Watch "Validate input" New topic
Author

Validate input

sae0203
Ranch Hand

Joined: Sep 19, 2002
Posts: 34
Help is needed.
I need to do a webpage which include jsp code that validate the user input at the text area. i have read through some jsp tutorial but i am still unclear of how the codes works.
Below is the code which i had to just get the input and display it but what should i add to ask it to validate the input like lets say i only want user to input the character a,c,g,t if an invalid input is detected, it will display an error msg.
<html>
<head>
<Title>Testing</title>
</head>
<body>
<h1> Validation Testing</h1>
You typed :
<%
StringBuffer text = new StringBuffer(request.getParameter("txtarea1"));
int loc = (new String(text)).indexOf('\n');
while (loc>0) {
text.replace(loc, loc+1, "<BR>");
loc = (new String(text)).indexOf('\n');
}
//String seqArray[] = new String[100];
//seqArray = text;
out.println(" " + text);
//out.println("The array output: " + seqArray);
%>
</body>
</html>


SAE <br /> <br /><a href="http://www.findsingapore.net/forum" target="_blank" rel="nofollow">http://www.findsingapore.net/forum</a>
Paul Kelcey
Ranch Hand

Joined: Jan 26, 2003
Posts: 45
Why don't you use JavaScript to do the validation on the client-side? Your JSP would include the necessary JavaScript. When the user goes to submit the form the script would check that the format is correct. If the format was invalid then it would show an error message. This is more user-friendly since the user has immediate feed-back as to the problem and doesn't have to wait for a request/response.
PK
Nischal Tanna
Ranch Hand

Joined: Aug 19, 2003
Posts: 182
hi..
I have a very small code..which i guess may help u a bit..
I m checking for a character in a String..
public class test
{
public static void main(String args[])
{
String str="abc";
char c='a';//character to be checked.It can be also a String
if(str.indexOf(c)==-1)
{
System.out.println("character not present");
}
else
{
System.out.println("index "+str.indexOf(c));
}
}
}


Thnx
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Originally posted by Paul Kelcey:
Why don't you use JavaScript to do the validation on the client-side? Your JSP would include the necessary JavaScript. When the user goes to submit the form the script would check that the format is correct. If the format was invalid then it would show an error message. This is more user-friendly since the user has immediate feed-back as to the problem and doesn't have to wait for a request/response.
PK

Yes, try to achieve this minor things on the client side, before sending the request. But you should not rely only on javascript. The user can easily disable it. So, in addition, you should also do server side validation. Have a look at
this article.
HTH


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Validate input
 
Similar Threads
Servlet becomming slow
Servlet becomming slow
Passing parameter to servlet from another servlet
tomcat can't find index.jsp
Folder Creation On Server