• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Validate input

 
sae0203
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
Paul Kelcey
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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));
}
}
}
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic