I use this function to add rows dynamically in a table
function addRow()
{
var tbl = document.getElementById('applications');
var lastRow = tbl.rows.length;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var row = tbl.insertRow(lastRow);
// right cell
var cellRight = row.insertCell(0);
var el = document.createElement('input');
el.type = 'text';
el.name = 'application_n' + iteration;
el.id = 'application_n' + iteration;
el.size = 45;
el.className='cellData';
el.style.width='220px'
el.style.height='17px'
cellRight.appendChild(el);
alert("the id is " + el.id)
var cellMiddle = row.insertCell(1);
var fl = document.createElement('input');
fl.type = 'text';
fl.name = 'application_no' + iteration;
fl.id = 'application_no' + iteration;
fl.size = 45;
fl.className='cellData';
fl.style.width='220px'
fl.style.height='17px'
cellMiddle.appendChild(fl);
var cellLeft = row.insertCell(2);
var gl = document.createElement('input');
gl.type = 'text';
gl.name = 'application_doj' + iteration;
gl.id = 'application_doj' + iteration;
gl.size = 45;
gl.className='cellData';
gl.style.width='220px'
gl.style.height='17px'
gl.value='mm/dd/yyyy'
gl.onfocus= function() {gl.value=""};
gl.onblur= function() {ValidateForm()};
cellLeft.appendChild(gl);
document.getElementById("no_of_applications").value=iteration
}
and using this function to validate date ( the column created as gl in the above function )
var dtCh= "/";
var minYear=1900;
var maxYear=2100;
function isInteger(s){
var i;
for (i = 0; i < s.length; i++){
// Check that current character is number.
var c = s.charAt(i);
if (((c < "0") || (c > "9"))) return false;
}
// All characters are numbers.
return true;
}
function stripCharsInBag(s, bag){
var i;
var returnString = "";
// Search through string's characters one by one.
// If character is not in bag, append to returnString.
for (i = 0; i < s.length; i++){
var c = s.charAt(i);
if (bag.indexOf(c) == -1) returnString += c;
}
return returnString;
}
function daysInFebruary (year){
// February has 29 days in any year evenly divisible by four,
// EXCEPT for centurial years which are not also divisible by 400.
return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
for (var i = 1; i <= n; i++) {
this[i] = 31
if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
if (i==2) {this[i] = 29}
}
return this
}
function isDate(dtStr){
var daysInMonth = DaysArray(12)
var pos1=dtStr.indexOf(dtCh)
var pos2=dtStr.indexOf(dtCh,pos1+1)
var strMonth=dtStr.substring(0,pos1)
var strDay=dtStr.substring(pos1+1,pos2)
var strYear=dtStr.substring(pos2+1)
strYr=strYear
if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
for (var i = 1; i <= 3; i++) {
if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
}
month=parseInt(strMonth)
day=parseInt(strDay)
year=parseInt(strYr)
if (pos1==-1 || pos2==-1){
document.getElementById("DOJ_error").innerHTML = "please enter a date in - mm/dd/yyyy format"
return false
}
if (strMonth.length<1 || month<1 || month>12){
document.getElementById("DOJ_error").innerHTML = "Please enter a valid month"
return false
}
if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
document.getElementById("DOJ_error").innerHTML = "Please enter a valid day"
return false
}
if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
document.getElementById("DOJ_error").innerHTML = "Please enter a valid 4 digit year"
return false
}
if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
document.getElementById("DOJ_error").innerHTML = "Please enter a valid date"
return false
}
return true
}
function ValidateForm()
{
alert("hi from ValidateForm")
//var dt=document.transaction.application_doj' + iteration
//alert(dt)
iteration=document.getElementById("no_of_applications").value
alert(iteration)
var dt=document.getElementById("application_doj" + iteration).value
// var dt=document.getElementById("application_doj2").value
alert(dt)
if (isDate(dt.value)==false){
//dt.focus()
return false
}
else
{
document.getElementById("DOJ_error").innerHTML=""
return true
}
}
tihs is the DOJ_error
<span id="DOJ_error"></span>
but nothing happens when i run this...
it does gets the value from each date column in the table and also calls the isDate() function
but nothing after that...