Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

javascript

Status
Not open for further replies.

techskool

Technical User
Jun 20, 2002
151
GB
When i use a 'file field' on a form to browse for an file, i want to just be able to select *.jpg's, so a user cannot select any file other than a jpg (or perhaps even see anyfile other than a jpg)

How would i go about this?

Thanx

Dave

:)
 
Best to catch the onSubmit event, check the make sure that field ends in '.jpg' and if it doesn't alert the user and cancel the submit by returning false. If it matches the do a form.submit.

You won't be able to limit thte file browser with JS, thats an OS function.
 
How's this?

<html>
<head>
<script>
function checkFile(f){
v=f.fileField.value;
if(v.substring(v.lastIndexOf('.')).toLowerCase()!='.jpg'){
alert('Files must be .jpg');
return false;
}
return true;
}
</script>
<body>
<form onsubmit=&quot;return checkFile(this)&quot;>
<input type=&quot;file&quot; name=&quot;fileField&quot;>
<input type=&quot;submit&quot;>
</form>
</body>
</html>

Adam
while(ignorance==true){perpetuate(violence,fear,hatred);life--};
 
Thanks guys

Im very new to Javascript, i usually use ASP

Ive tried to use your ideas in the current function that happens on submitt but im getting an error.

Whats wrong please:
----------------------------------------
function onSubmitForm() {
var formDOMObj = document.frmSend;
if (formDOMObj.attach1.value != &quot;&quot; )

if (formDOMObj.attach1.value =='.jpg')
return true;
else
alert('Files must be .jpg');
return false;

else
alert(&quot;Please press the browse button and pick a file.&quot;);
return false;
-----------------

Thanks

Dave:)


}

 
Please ignore my ignorance

i really should have known how to do this, all coding is similar

Im using:

-----------------
function onSubmitForm() {
var formDOMObj = document.frmSend;
var v=formDOMObj.attach1.value;

if (formDOMObj.attach1.value == &quot;&quot;)
alert(&quot;Please press the browse button and pick a file.&quot;)
else
if(v.substring(v.lastIndexOf('.')).toLowerCase()=='.jpg')
return true;
else
alert('Files must be .jpg');
return false;
return false;
}
--------------


Thanks for the advice

Dave
:)
 
You're getting an error with that code? Or do you mean that you've fixed it?

If you're getting an error: what is it? If not: well done!

(NB -- not everyone has javascript enabled.)
 
No i fixed it mate,..i was just posting it cos i figured it out after having said i could work it out!

Thanks for all the help people

Dave

:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top