How can I add my validation script to this file upload code?
Validation Script:[code=javascript]function validate(frm) {
if (!/(\.(gif|jpg|jpe g|png))$/i.test(frm.file .value)){
alert('Invalid file type.\nPlease attach a valid image file');
frm.file.focus( );
return false;
}
return true;
}[/code]
Upload Script[code=javascript]function startCallback() {
//VALIDATION SCRIPT NEEDS TO BE ADDED HERE
}
function completeCallbac k(response) {
dcdRes(response );
}
AIM = {
frame : function(c) {
var n = 'f' + Math.floor(Math .random() * 99999);
var d = document.create Element('DIV');
d.innerHTML = '<iframe style="display: none;" src="about:blan k" id="'+n+'" name="'+n+'" onload="AIM.loa ded(\''+n+'\')" ></iframe>';
document.body.a ppendChild(d);
var i = document.getEle mentById(n);
if (c && typeof(c.onComp lete) == 'function') {
i.onComplete = c.onComplete;
}
return n;
},
form : function(f, name) {
f.setAttribute( 'target', name);
},
submit : function(f, c) {
AIM.form(f, AIM.frame(c));
if (c && typeof(c.onStar t) == 'function') {
return c.onStart();
} else {
return true;
}
},
loaded : function(id) {
var i = document.getEle mentById(id);
if (i.contentDocum ent) {
var d = i.contentDocume nt;
} else if (i.contentWindo w) {
var d = i.contentWindow .document;
} else {
var d = window.frames[id].document;
}
if (d.location.hre f == "about:blan k") {
return;
}
if (typeof(i.onCom plete) == 'function') {
i.onComplete(d. body.innerHTML) ;
}
}
}//got the code from www.webtoolkit. info[/code]
HTML File[html]<form action="upload. php" method="post" onsubmit="retur n AIM.submit(this , {'onStart' : startCallback, 'onComplete' : completeCallbac k})" enctype="multip art/form-data">
<input type="file" name="file" />
<input type="submit" name="submit" value="Upload" />
</form>[/html]
Its working too good, but I need the validation first.
Validation Script:[code=javascript]function validate(frm) {
if (!/(\.(gif|jpg|jpe g|png))$/i.test(frm.file .value)){
alert('Invalid file type.\nPlease attach a valid image file');
frm.file.focus( );
return false;
}
return true;
}[/code]
Upload Script[code=javascript]function startCallback() {
//VALIDATION SCRIPT NEEDS TO BE ADDED HERE
}
function completeCallbac k(response) {
dcdRes(response );
}
AIM = {
frame : function(c) {
var n = 'f' + Math.floor(Math .random() * 99999);
var d = document.create Element('DIV');
d.innerHTML = '<iframe style="display: none;" src="about:blan k" id="'+n+'" name="'+n+'" onload="AIM.loa ded(\''+n+'\')" ></iframe>';
document.body.a ppendChild(d);
var i = document.getEle mentById(n);
if (c && typeof(c.onComp lete) == 'function') {
i.onComplete = c.onComplete;
}
return n;
},
form : function(f, name) {
f.setAttribute( 'target', name);
},
submit : function(f, c) {
AIM.form(f, AIM.frame(c));
if (c && typeof(c.onStar t) == 'function') {
return c.onStart();
} else {
return true;
}
},
loaded : function(id) {
var i = document.getEle mentById(id);
if (i.contentDocum ent) {
var d = i.contentDocume nt;
} else if (i.contentWindo w) {
var d = i.contentWindow .document;
} else {
var d = window.frames[id].document;
}
if (d.location.hre f == "about:blan k") {
return;
}
if (typeof(i.onCom plete) == 'function') {
i.onComplete(d. body.innerHTML) ;
}
}
}//got the code from www.webtoolkit. info[/code]
HTML File[html]<form action="upload. php" method="post" onsubmit="retur n AIM.submit(this , {'onStart' : startCallback, 'onComplete' : completeCallbac k})" enctype="multip art/form-data">
<input type="file" name="file" />
<input type="submit" name="submit" value="Upload" />
</form>[/html]
Its working too good, but I need the validation first.
Comment