Friends i got quite some success but at the replace through an undefined error:
here is my new code:
var avidno = '(800)123 1234';
var bodytext = document.body.innerHTML;
function validate () {
var regex = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/gi;
if (regex.test(avidno)) {
alert('bingo');
var altrstr = '<span>'+avidno+'</span>';
//var newaltr = bodytext.replace(avidno, altrstr);
//var str_count = bodytext.match(avidno).length; //4 matched
document.body.innerHTML = newaltr;
alert(avidno.length);
find_count = avidno.length;
for(var i=0;i<find_count;i++)
{
var newaltr = bodytext.replace(avidno, altrstr);
}
// Valid international phone number
} else {
alert('uupss');
// Invalid international phone number
}
}
validate();
bodytext(and thereforedocument.body.innerHTML) is undefined. Make sure you run the code when the document is loaded. Providing the exact error message wouldn't be wrong either...