index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Registration</title>
</head>
<body>
<<h1>Registration Form</h1>
<form action="Validate.jsp" method="post"
<table style="width:50%">
<tr>
<td>Full Name</td>
<td><input type="text" name="fullname"</td>
<br/><br/>
</tr>
<tr>
<td>Age</td>
<td><input type="text" name="age"</td>
<br/><br/>
</tr>
<tr>
<td>Email</td>
<td><input type="email" name="email" size="20"></td>
<br/><br/>
</tr>
<tr>
<td><Gender</td>
<td><input type="radio" name="gender" value="Male">Male
<input type="radio" namae="gender" value="Female">Female
<br/><br/>
</td>
</tr>
<tr>
<td>Hobbies</td>
<td>
<input type="checkbox" name="hb" value="Acting"/>Acting
<input type="checkbox" name="hb" value="Dancing"/>Dancing
<input type="checkbox" name="hb" value="Singing"/>Singing
<br/><br/>
</td>
</tr>
</table>
<br/><br/>
<input type="submit" value="Register"/>
</form>
</body>
</html>
Validate.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<h1>Validation Page</h1>
<%!
int ageInNumbers;
private static final String EMAIL_REGEX="^[\\w-\\+]+(\\.[\\w]+)*@[\\w-]+(\\.[\\w]+)*(\\.[az]{2,})$";
%>
<%
String name=request.getParameter("fullname");
String age=request.getParameter("age");
String email=request.getParameter("email");
String gender=request.getParameter("gender");
String hb[]=request.getParameterValues("hb");
if(name.isEmpty()||age.isEmpty()||email.isEmpty()||gender.isEmpty())
{
out.println("<font color=red>Please fill all the fields</font><br>");
}
if(!email.matches(EMAIL_REGEX))
{
out.println("<font color=red>Correct your Email address</font><br>");
} try
{
ageInNumbers=Integer.parseInt(age.trim());
}
catch (NumberFormatException e)
{
out.println("<font color=red>Age must be in numbers</font><br>");
}
if(ageInNumbers>=18&&ageInNumbers>=60)
{
out.println("<font color=red>Age must be between 18 to 60</font><br>");
}
%>
Your Entered Information is as follows:<br><br>
Full Name <b>:<%=name%></b><br>
Age <b>:<%=age%></b><br>
Email <b>:<%=email%></b><br>
Gender <b>:<%=gender%></b><br>
Hobbies<b>:
<%
if(hb != null && hb.length!=0)
{
for(int i=0;i<hb.length;i++)
{
out.println(hb[i]);
}
} %>
</body>
</html>
Output:-
0 Comments