Header Ad

Friday, September 5, 2008

JAVASCRIPT DATE VALIDATION

The following is the link for Date Validation i.e. Entered date is valid r not.
 
http://www.expertsrt.com/scripts/Rod/validate_date.php
 
 
function validateDate(fld) {
    var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
    var errorMessage = 'Please enter valid date as month, day, and four digit year.\nYou may use a slash, hyphen or period to separate the values.\nThe date must be a real date. 2-30-2000 would not be accepted.\nFormay mm/dd/yyyy.';
    if ((fld.value.match(RegExPattern)) && (fld.value!='')) {
        alert('Date is OK'); 
    } else {
        alert(errorMessage);
        fld.focus();
    } 
}

Saturday, August 30, 2008

JAVASCRIPT REGULAR EXPRESSION FOR REMOVING SPECIAL SYMBOLS

The following example will give the you the notes to replace the special characters in a string.

Place the following coed with in the script tags.

var temp1;

var temp = new String('Thi\'s is a te!!!!s$t st>ri9ng... S"o??? What#...');

document.write(temp + '
');

temp = temp.replace(/[^a-zA-Z 0-9]+/g,'-');

document.write(temp + '
');

temp1=temp.replace(/\s/g,'-');

document.write(temp1 + '
');

Saturday, January 19, 2008

Date Difference between Two Dates

The following program is to get the Date Difference in days.Here we have to provide two date fields which are of date time type(contains both date and time).


import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Vector;



public class DateDifference
{
public static int getDaysinMonth(int year, int month)
{
int days=0;
int nly[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int ly[] = {31,29,31,30,31,30,31,31,30,31,30,31};
int y[];
if (year%4 == 0)
y=ly;
else
y=nly;

for (int intIndex=0;intIndex<month;intIndex++)
{
days = days+y[intIndex];
}
return days;
}

public static long getDays(String date[])
{
long days = 0;
int date1[]={0,0,0};
for (int intIndex=0;intIndex<date.length;intIndex++)
{
date1[intIndex] = Integer.parseInt(date[intIndex].trim());
}
days = (((date1[0]-1)/4)+1)*366 + ((date1[0]-1)-(((date1[0]-1)/4)+1)) * 365;
days = days + getDaysinMonth(date1[0],date1[1]-1);
days = days + (date1[2] - 1);
return days;
}

public static void main(String[] args)
{
long lngDateDiffinDays=0;
Date1=Date1.trim();
String date_time1[] = Date1.split(" ");
String date1[] = date_time1[0].split("-");
long days1 = getDays(date1);
Date2=Date2.trim();
String date_time2[] = Date2.split(" ");
String date2[] = date_time2[0].split("-");
long days2 = getDays(date2);

lngDateDiffinDays=days1-days2;
}

}

Thursday, January 17, 2008

Reading XLS Content Using Java Code.

Reading XLS Content Using Java Code.

We can read XLS content cell by cell by using java code.For this we need jxl jar file.

The following is the simple example to read .xls file and display its content.

Following is the link to download jxl jar file
http://fisheye1.cenqua.com/viewrep/jptools/jpTools/lib




import java.io.File;
import java.io.*;
import jxl.*;
import java.util.*;
import jxl.Workbook;
import jxl.read.biff.*;


class ReadXls
{
public static void main(String[] args)
{
try
{
String filename = "C:/ExcelFileName.xls";
WorkbookSettings ws = new WorkbookSettings();
ws.setLocale(new Locale("en", "EN"));

Workbook workbook = Workbook.getWorkbook( new File(filename),ws);

Sheet s = workbook.getSheet(0);
System.out.println("Sheet Content::"+s.getName());
readDataSheet(s);
workbook.close();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (BiffException e)
{
e.printStackTrace();
}
private static void readDataSheet(Sheet s)
{
// Find the labeled cell from sheet
System.out.println(lc.getString());

//gets the value of cell at specified column and row
DateCell dc = (DateCell) s.getCell(20,1);
System.out.println(dc);

}
}