Wednesday, February 21, 2007

The Javascript Form Validation

Here are some important form validation in java script which may help you when needed.
In this blog some example are there which can be custmized and used in diffrent programs.




1.Mandatory Field Validation:

Function()
{
var fieldLength=document.yourformname.yourfieldname.value.length;

if(fieldLength==0)
{

window.alert("Please Enter a value");
return false;
}

return true;
}

2.Length of Field Validation In javascript:

Function()
{
var fieldLength=document.yourformname.yourfieldname.value.length;

if(fieldLength>10fieldLength<4)
{

window.alert("Entered value is not of proper length");
return false;
}

return true;
}



3.Email Validation in javascript:


function echeck()
{

var at="@"
var dot="."
var email=document.yourformname.yourfieldname.value;

var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
if (str.indexOf(at)==-1)
{
alert("Invalid E-mail ID")

return false

}


if (str.indexOf(at)==-1 str.indexOf(at)==0 str.indexOf(at)==lstr)
{
alert("Invalid E-mail ID")
return false
}
if (str.indexOf(dot)==-1 str.indexOf(dot)==0 str.indexOf(dot)==lstr){ alert("Invalid E-mail ID")
return false
}
if (str.indexOf(at,(lat+1))!=-1)
{
alert("Invalid E-mail ID")
return false
}
if (str.substring(lat-1,lat)==dot str.substring(lat+1,lat+2)==dot)
{
alert("Invalid E-mail ID")
return false
}
if (str.indexOf(dot,(lat+2))==-1)
{
alert("Invalid E-mail ID")
return false
}
if (str.indexOf(" ")!=-1)
{
alert("Invalid E-mail ID")
return false
}
return true
}
function ValidateForm()
{
var emailID=document.frmSample.txtEmail
if ((emailID.value==null)(emailID.value==""))
{ alert("Please Enter your Email ID") emailID.focus()
return false
}
if (echeck(emailID.value)==false)
{
emailID.value=""
emailID.focus()
return false
}
return true
}



4.Date Validation in javascript(Format:dd/month//yy):


function validDate()
{
var val=TravelInfoBean.date.value;
var val1=parseInt(val.charAt(0)+val.charAt(1));var val2=val.charAt(2);

var val3=val.charAt(3)+val.charAt(4)+val.charAt(5);var val4=val.charAt(6);
var val5=parseInt(val.charAt(7)+val.charAt(8));

if(val.length!=9)
{
window.alert("Invalid Date");
return false;
}
if(isNaN(val1))
{
window.alert("Invalid Day1");return false;
}

if(val1<1>31)
{
window.alert("Invalid Day");
return false;
}

if(isNaN(val5))
{
window.alert("Invalid Year");
return false;
}
if(val5<0>99)
{
window.alert("Invalid Year"+val5);
return false;
}

if(val2!='-')
{
window.alert("Invalid Format");
return false;
}
if(val4!='-')
{
window.alert("Invalid Format");
return false;
}

if(val3!="JAN" && val3!="FEB" && val3!="MAR" && val3!="APR" && val3!="MAY" && val3!="JUN" && val3!="JUL" && val3!="AUG" && val3!="SEP" && val3!="OCT" && val3!="NOV" && val3!="DEC")
{
window.alert("Invalid Month");
return false;
}


return true;
}





Convert text file into PDF format in java

Text to PDF conversion:
For converting a text document into PDF format we have a java API called iText.
It is freely available on at http://www.lowagie.com/iText/download.html. . Some free Ebooks are also available in the same site.
Introduction:
iText is a library which creates PDF files dynamically.It helps a developer to extends his capability on their web server.It is very easy to integrate iText solution to servlet or jsp programme.
Example:
Here is a example to to convert a file called niaz.txt to PDF format when it is executed.
package pdf;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Image;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfWriter;
import java.io.FileOutputStream;
import java.io.BufferedReader;
import java.io.FileReader;

class Main //class pdfopen
{
public static void main(String args[])
//main function
{
try //try statement
{
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream"D:\\ITextTest.pdf"));
document.open();
document.add(new Paragraph("hi hello how r u"));
FileReader fr = new FileReader("D:\\TRIANGLE\\niaz.txt");
BufferedReader br = new BufferedReader(fr);
String record=new String();

while(br.readLine()!=null)
{
document.add(new Paragraph(br.readLine()));
}
document.close();
System.out.println("closed");
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "D:\\ITextTest.pdf");
}
catch (Exception e)
{
System.out.println("Error" + e.getMessage() );
e.printStackTrace();//print the error
}
}
}