| Piece Of Meat |
10-09-2012 07:44 PM |
I make sure that the id of the table is "mytable" and copied your code so now my js file looks like this:
Code:
function addLoadListener(fn)
{
if (typeof window.addEventListener != 'undefined')
{
window.addEventListener('load', fn, false);
}
else if (typeof document.addEventListener != 'undefined')
{
document.addEventListener('load', fn, false);
}
else if (typeof window.attachEvent != 'undefined')
{
window.attachEvent('onload', fn);
}
};
function attachEventListener(target, eventType, functionRef, capture)
{
if (typeof target.addEventListener != "undefined")
{
target.addEventListener(eventType, functionRef, capture);
}
else
{
target.attachEvent("on" + eventType, functionRef);
}
return true;
};
function validateForm()
{
attachEventListener(document.getElementById('addform'), "submit", validateadd, false);
function validateadd()
{
return checking('addfirst','addlast','addmail','addnumber');
}
attachEventListener(document.getElementById('editform'), "submit", validateedit, false);
function validateedit()
{
return checking('editfirst','editlast','editmail','editnumber');
}
var therows=document.getElementById("mytable").rows;
for (var i = 0; i < therows.length; i++) {
attachEventListener(therows[i],'mouseover',function(){this.style.backgroundColor="red"});
attachEventListener(therows[i],'mouseout',function(){this.style.backgroundColor="white"});
}
};
function checking(first, last, mail, number)
{
var firstvalue=document.getElementById(first).value;
var lastvalue=document.getElementById(last).value;
var mailvalue=document.getElementById(mail).value;
var numbervalue=document.getElementById(number).value;
var shtrodel=0;
var error=0;
var dot=0;
//firstname validation
for (counter=0;counter<firstvalue.length;counter++)
{
if(!(firstvalue.charAt(counter)>='a' && firstvalue.charAt(counter)<='z')&&!(firstvalue.charAt(counter)>='A' && firstvalue.charAt(counter)<='Z')
&&firstvalue.charAt(counter)!=' ')
{
alert ("First name must contain only letters!")
return false;
}
}
firstvalue=capitaliseFirstLetter(firstvalue);
document.getElementById(first).value=firstvalue;
//lastname validation
for (counter=0;counter<lastvalue.length;counter++)
{
if(!(lastvalue.charAt(counter)>='a' && lastvalue.charAt(counter)<='z')&&!(lastvalue.charAt(counter)>='A' && lastvalue.charAt(counter)<='Z')
&&lastvalue.charAt(counter)!=' ')
{
alert ("Last name must contain only letters!")
return false;
}
}
lastvalue=capitaliseFirstLetter(lastvalue);
document.getElementById(last).value=lastvalue;
//mail validation
for (counter=0;counter<mailvalue.length;counter++)
{
if(!(mailvalue.charAt(counter)>='a' && mailvalue.charAt(counter)<='z')&&!(mailvalue.charAt(counter)>='A' && mailvalue.charAt(counter)<='Z')
&&!(mailvalue.charAt(counter)>='0'&&mailvalue.charAt(counter)<='9')
&& mailvalue.charAt(counter)!='_' && mailvalue.charAt(counter)!='-' && mailvalue.charAt(counter)!='.' && mailvalue.charAt(counter)!='@')
{
error=1;
}
if(mailvalue.charAt(counter)=='@')
{
shtrodel++;
}
if(mailvalue.charAt(counter)=='.')
{
dot++;
}
}
if (error==1)
{
alert("Illegal e-mail adress: The adress should contain only letters, '-', '_' or '.'");
return false;
}
if (dot==0)
{
alert("Illegal e-mail adress");
return false;
}
if (shtrodel==0)
{
alert("Illegal e-mail adress: The adress must contain @");
return false;
}
else if (shtrodel>1)
{
alert("Illegal e-mail adress: The adress must contain only one '@'");
return false;
}
//number validation
for (counter=0;counter<numbervalue.length;counter++)
{
if(!(numbervalue.charAt(counter)>='0'&&numbervalue.charAt(counter)<='9'))
{
alert("Illegal phone number: The phone number must contain only numbers!");
return false;
}
}
if (numbervalue.length<8)
{
alert("The phone number is too short");
return false;
}
else if (numbervalue.length>12)
{
alert("The phone number is too long");
return false;
}
return true;
};
function capitaliseFirstLetter(string)
{
return string.charAt(0).toUpperCase() + string.slice(1);
}
addLoadListener(validateForm);
And it's still not working. Do you have any idea why?
|