Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

How do i call servlet from javascript after validation the javascript

843841Sep 28 2007 — edited Sep 28 2007
Hi ,

Can anyone tell me how to call a servlet after the javascript is being validated. Here is my code to validate javascript i need to call a servlet inorder to save the data into the same form. I tried calling through the action method in the form but its directly taking me to the servlet but not validating the form. Is there any way that i can validate the form first then call the servlet.

Thanks in advance.

<html>
<head>

<title>Online Blog</title>

<script language ="javascript" type ="text/javascript">

function myfunction()

{

var mesg = "";
var dmesg = "";
var dnumber = 0;
var number = 0
var Blogstr = document.onlineblog.BlogName;
var Fnamestr = document.onlineblog.FirstName;
var Lnamestr = document.onlineblog.LastName;
var Datestr = document.onlineblog.Dateformat;
var Imagestr = document.onlineblog.uploadimage;
var Imageval = Imagestr.value;
var flength = parseInt(Imageval.length) - 3;
var fext = Imageval.substring(flength,flength + 3);

var Dateval = Datestr.value;
var Dformat = /^\d{2}\/\d{2}\/\d{4}/; //checks the date format.

//splits date into mm, dd , yyyy format.

var m = Dateval.split("/")[0];
var d = Dateval.split("/")[1];
var y = Dateval.split("/")[2];

//Get today's date (removes time).

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();

//checks if month and date are displayed in mm, dd format and if not sets to mm,dd.

if (mm <10)
{
mm = '0'+mm;
}

if (dd <10)
{
dd = '0'+dd;
}

//checks if name of the blog is empty

if(Blogstr.value == "")
{
number = number+1;
mesg += "\n" + number + "Name of the blog";
}

//checks if value of Frist Name is empty;

if(Fnamestr.value == "")
{
number = number+1;
mesg += "\n" + number + "First Name";
}

//checks if value of Last Name is empty;

if(Lnamestr.value == "")
{
number = number+1;
mesg += "\n" + number + "Last Name";
}

//checks if value of date is empty;

if(Datestr.value == "")
{
number = number+1;
mesg += "\n" + number + "Date of mm/dd/yyyy format";
}

//checks if value of image field is empty;

if(Imagestr.value == "")
{
number = number+1;
mesg += "\n" + number + "select an image";
}

//displays all the error messages.

if (number > 0)
{
alert ("Please enter the following" + mesg);
return false;
}

//checks if entered date format is mm/dd/yyyy

if(!Dformat.test(Dateval))
{
dnumber = dnumber +1;
dmesg += "\n" + dnumber + "please enter a valid date(mm/dd/yyyy)";
}

//checks if date is greater than 12 and is 2 digits.

if ((m>12 && m<100) || (m> = 100 && m<1000) || (m==0))
{
dnumber = dnumber+1;
dmesg += "\n" + dnumber + "Enter valid month(mm)";
}

//checks if month has 31 or 30 days and is in dd format.

if (((d>30) && (d<100) && (m == 04 || m == 06 || m == 9 || m == 11)) || ((m == 01 || m == 03 || m == 05 || m == 07 || m == 08 || m == 10 || m==12) && (d >31) && (d<100)) || d == 0)
{
dnumber = dnumber+1;
dmesg += "\n" + dnumber + "Enter valid date(dd)";
}

//checks if month has 28 or 29 days and wheather is a leap year or not.
if((m == 02 && d>29 && y%4 == 0) || (m == 02 && d>28 && y%4 != 0))
{
dnumber = dnumber+1;
dmesg += "\n" + dnumber + "Enter valid date(dd)";
}


//checks if entered date is prior to the current date.
if((m == mm && d == dd && y == yyyy) || (y > yyyy)|| (d>=dd && m>=mm && m<=12 && y==yyyy) || (d<=dd && m>mm && y == yyyy && m<=12))
{
dnumber = dnumber + 1;
dmesg += "\n" + dnumber + "Please enter a date prior to the current date";
}

if (y == 0 || y < 1000 || y>9999) //checks if year is zero and is a 4 digit number.
{
dnumber = dnumber + 1;
dmesg += "\n" + dnumber + "Enter a valid year(yyyy)";
}

//checks if image format is jpg or gif.

if(fext != "jpg" && fext != "gif")
{
dnumber = dnumber + 1
dmesg += "\n" + dnumber + "You can only upload gif or jpg images.";
}


if(dnumber>0)
{
alert("please check the followin error messages"+dmesg);
return false;
}


}

</script>

<style type="text/css">

.style1 {color: #FF0000}
body {
background-color: #FFCCFF;
}
h2 {
color: #CC3399;
}
h1 {
color: cc3399;
}
.style2 {
font-family: Arial, Helvetica, sans-serif;
font-weight: bold;
}
.style5 {color: #000000; font-family: "Times New Roman", Times, serif;}

</style>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><body>

<h1 align="center">Webloggers space</h1>

<p align="center">Home
<p>
<h4> Please enter the following details to register.</h4>
<p> <b>Note:</b> All fields marked with <span class="style1">*</span>(astrick)
are compulsory.</p>
<form action="servlet/BlogServlet" name="onlineblog" onsubmit = "return myfunction()" method="post">
<table width="60%" border="0" cellspacing="5" cellpadding="2">

<tr>
<td><span class="style1">*</span>Name of the Blog</td>
<td><input type="text" name="BlogName"/></td>
</tr>
<tr>
<td><span class="style1">*</span>First Name</td>
<td><input type="text" name="FirstName"/></td>

</tr>
<tr>
<td><span class="style1">*</span>Last Name</td>
<td><input type="text" name="LastName"/></td>
</tr>
<tr>
<td><span class="style1">*</span>Date(mm/dd/yyyy)</td>

<td><input type="text" name="Dateformat"/></td>
</tr>
<tr>
<td>Category</td>
<td><select name="catagory" size="1">
<option></option>
<option>Business</option>
<option>Education</option>

<option>Entertainment</option>
<option>Food</option>
<option>Hobbies</option>
<option>Personal</option>
<option>Politics</option>
<option>Sports</option>

</select></td>
</tr>
<tr>
<td>Enter your text here:</td>
<td><textarea name="textarea" cols="45" rows="5"></textarea></td>
</tr>
<tr>
<td><span class="style1">*</span>Upload Image <span class="style5">(.jpg
or .gif)</span></td>

<td><input type="file" name="uploadimage" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="save">
<input type="reset" name="reset" value="Reset" /></td>
</tr>
</table>
</form>

</body>
</html>
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 26 2007
Added on Sep 28 2007
1 comment
279 views