Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Old 02-24-2006, 07:40 AM File extension check
Rickzkm's Avatar
Average Talker

Posts: 15
Location: UK
Trades: 0
Hi there,
I would need javascript code to check extension of uploaded file.
File upload in not mandatory, but I need script to check extension and allow only JPG, BMP, PGN and other Image files.

Link to the example: http://www.empire-elements.co.uk/mdf...line-form.html

Thank you all
__________________

Please login or register to view this content. Registration is FREE
Rickzkm is offline
Reply With Quote
View Public Profile Visit Rickzkm's homepage!
 
 
Register now for full access!
Old 02-24-2006, 09:33 AM Re: File extension check
Anacrusis's Avatar
Defies a Status

Posts: 2,099
Name: Adam
Location: Colchester CT
Trades: 0
Try this:
Put this code in the head section of your page
Code:
<script language="JavaScript">
    extArray = new Array(".jpg", ".png", ".bmp");
    function LimitAttach(form, file) {
    allowSubmit = false;
    if (!file) return;
    while (file.indexOf("\\") != -1)
    file = file.slice(file.indexOf("\\") + 1);
    ext = file.slice(file.indexOf(".")).toLowerCase();
    for (var i = 0; i < extArray.length; i++) {
    if (extArray[i] == ext) { allowSubmit = true; break; }
    }
    if (allowSubmit) return true;
    else
    alert("Please only upload files that end in types:  "
    + (extArray.join("  ")) + "\nPlease select a new "
    + "file to upload and submit again.");
    return false;
    }
    //  End -->
</script>
Use this for your submit button on your upload form:
Code:
<input type='submit' name='Submit' value='Upload' onclick='return LimitAttach(this.form, this.form.FILE_FIELD.value)
Change FILE_FIELD to the name of the upload field on your form.
Anacrusis is offline
Reply With Quote
View Public Profile
 
Old 02-24-2006, 09:52 AM Re: File extension check
Rickzkm's Avatar
Average Talker

Posts: 15
Location: UK
Trades: 0
Could you please help me to add it to existing javascript that i have for this form ?

function validate(f)
{
if (f.reg1.value=="") {
alert("Please add Vinyl Finishes!");
f.reg1.focus();
return false;
}
if (f.reg2.value=="") {
alert("Please add Rout Styles Doors!");
f.reg2.focus();
return false;
}
if (f.reg3.value=="") {
alert("Please add Rout Styles Drawer Fronts!");
f.reg3.focus();
return false;
}
var a = parseInt(f.phone.value,10);
if (! a || a <= 0 ) {
alert("Please put your telephone number!");
f.phone.focus();
return false;
}
if (f.delivery.value=="") {
alert("Please add delivery address!");
f.delivery.focus();
return false;
}
if(f.pravidla.checked == false){
alert("Sorry, you must confirm that order details are correct");
f.pravidla.focus();
return false;
}
if(f.pravidla2.checked == false){
alert("Sorry, you must agree to our terms and conditions");
f.pravidla2.focus();
return false;
}
if(f.name.value == "") {
alert("Please add your name");
f.name.focus();
return false;
}
charset=new RegExp("^[^.]+(\.[^.]+)*@([^.]+[.])+[a-z]{2,3}$");
if (!charset.test(f.email.value))
{
window.alert("Please add valid email address!");
f.email.focus();
return false;
}

return true;
}
__________________

Please login or register to view this content. Registration is FREE
Rickzkm is offline
Reply With Quote
View Public Profile Visit Rickzkm's homepage!
 
Old 02-24-2006, 09:57 AM Re: File extension check
Anacrusis's Avatar
Defies a Status

Posts: 2,099
Name: Adam
Location: Colchester CT
Trades: 0
Code:
function validate(f) 
{
if (f.reg1.value=="") {
alert("Please add Vinyl Finishes!");
f.reg1.focus();
return false;
} 
if (f.reg2.value=="") {
alert("Please add Rout Styles Doors!");
f.reg2.focus();
return false;
} 
if (f.reg3.value=="") {
alert("Please add Rout Styles Drawer Fronts!");
f.reg3.focus();
return false;
} 
var a = parseInt(f.phone.value,10);
if (! a || a <= 0 ) {
alert("Please put your telephone number!");
f.phone.focus();
return false;
} 
if (f.delivery.value=="") {
alert("Please add delivery address!");
f.delivery.focus();
return false;
} 
if(f.pravidla.checked == false){
alert("Sorry, you must confirm that order details are correct");
f.pravidla.focus();
return false;
} 
if(f.pravidla2.checked == false){
alert("Sorry, you must agree to our terms and conditions");
f.pravidla2.focus();
return false;
} 
if(f.name.value == "") {
alert("Please add your name");
f.name.focus();
return false;
}
charset=new RegExp("^[^.]+(\.[^.]+)*@([^.]+[.])+[a-z]{2,3}$");
if (!charset.test(f.email.value))
{
window.alert("Please add valid email address!");
f.email.focus();
return false;
}

return true; 
}


function LimitAttach(form, file) {
    extArray = new Array(".jpg", ".png", ".bmp");
    allowSubmit = false;
    if (!file) return;
    while (file.indexOf("\\") != -1)
    file = file.slice(file.indexOf("\\") + 1);
    ext = file.slice(file.indexOf(".")).toLowerCase();
    for (var i = 0; i < extArray.length; i++) {
    if (extArray[i] == ext) { allowSubmit = true; break; }
    }
    if (allowSubmit) return true;
    else
    alert("Please only upload files that end in types:  "
    + (extArray.join("  ")) + "\nPlease select a new "
    + "file to upload and submit again.");
    return false;
}
Anacrusis is offline
Reply With Quote
View Public Profile
 
Old 04-16-2009, 12:04 AM Re: File extension check
Banned

Posts: 81
Location: kavoir.com
Trades: 0
A rather useful bit, thanks! I recently got the same problem going on. In addition to checking the file extension prior to uploading, is it possible to use javascript to check the file size of the file to be uploaded? You know, web servers usually has this uploading limit I don't want my users to upload file that's too big and only after 20 minutes of uploading that they find out the uploading failed due to this.

I don't know much about javascript but my guess is that it's impossible as javascript is unable to access the file system? Or isit?
yangyang is offline
Reply With Quote
View Public Profile Visit yangyang's homepage!
 
Reply     « Reply to File extension check
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.52101 seconds with 12 queries