Go Back   Dynamic Drive Forums > General Coding > JavaScript
Search Dynamic Drive Forums:

Reply
 
Thread Tools Search this Thread
  #1  
Old 11-07-2009, 05:41 PM
lord22 lord22 is offline
Junior Coders
 
Join Date: Jul 2008
Posts: 81
Thanks: 38
Thanked 2 Times in 2 Posts
Default validate JPG

I have:
<input type="file" />
in my site with a submit button,
How can I validate that this is a .jpg file?

Thanks!
Reply With Quote
  #2  
Old 11-07-2009, 07:11 PM
Nile's Avatar
Nile Nile is offline
Elite Coders
 
Join Date: Jan 2008
Posts: 3,007
Thanks: 10
Thanked 442 Times in 438 Posts
Blog Entries: 5
Default

Add this to the <head> part of your document:
Code:
<script type="text/javascript">
function checkJPG(id){
var el = document.getElementById(id);
var patterna = /jpg$/i;
var patternb = /jpeg$/i;
if(!(patterna.test(el.value) || patternb.test(el.value))){
  alert("Your photo format is not a JPEG.");
}
};
</script>
And then add an ID attribute to your file tag, and on your submit button, add:
Code:
onclick="checkJPG('id');"
(Change the highlighted)

Good luck.
__________________
Coding is just another puzzle. And it's okay to have help putting together some peices.
Please don't be afraid to ask.
I try to highlight my code with correct syntax highlighting. Unless the its too long. Or I'm to busy.
Reply With Quote
The Following User Says Thank You to Nile For This Useful Post:
lord22 (11-07-2009)
  #3  
Old 11-07-2009, 07:35 PM
jscheuer1's Avatar
jscheuer1 jscheuer1 is offline
No Kidding?
 
Join Date: Mar 2005
Location: SE PA USA
Posts: 19,000
Thanks: 19
Thanked 1,135 Times in 1,121 Posts
Blog Entries: 3
Default

My version:

Code:
function valJpg(file){
	if(!/\.jpe{0,1}g$/i.test(file.value)){
		alert('.jpg Images Only Please.');
		var newFile = document.createElement('input');
		newFile.type = 'file';
		newFile.onchange = file.onchange;
		file.parentNode.replaceChild(newFile, file);
	}
}
HTML Code:
<input type="file" onchange="valJpg(this);">
You may need to include other attributes of your original file input if it has them, example:

Code:
function valJpg(file){
	if(!/\.jpe{0,1}g$/i.test(file.value)){
		alert('.jpg Images Only Please.');
		var newFile = document.createElement('input');
		newFile.type = 'file';
		newFile.name = file.name;
		newFile.onchange = file.onchange;
		file.parentNode.replaceChild(newFile, file);
	}
}
Which would be required for form submission, etc.
__________________
WWWWWWWWWWWW
- John
________________________

Really Show Your Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
Reply With Quote
The Following User Says Thank You to jscheuer1 For This Useful Post:
lord22 (11-07-2009)
  #4  
Old 11-07-2009, 09:30 PM
lord22 lord22 is offline
Junior Coders
 
Join Date: Jul 2008
Posts: 81
Thanks: 38
Thanked 2 Times in 2 Posts
Default

Thank you both
Reply With Quote
  #5  
Old 11-07-2009, 09:54 PM
jscheuer1's Avatar
jscheuer1 jscheuer1 is offline
No Kidding?
 
Join Date: Mar 2005
Location: SE PA USA
Posts: 19,000
Thanks: 19
Thanked 1,135 Times in 1,121 Posts
Blog Entries: 3
Default

I came up with another version which would preserve the idiosyncratic ways that input fields behave in various browsers, which may or may not be desirable, and also eliminates the need to add in various characteristics of the file input into the function:

Code:
function valJpg(file){
	if(!/\.jpe{0,1}g$/i.test(file.value)){
		alert('.jpg Images Only Please.');
		file.parentNode.replaceChild(valJpg.file.cloneNode(true), file);
	}
}
HTML Code:
<input id="myFile" type="file" onchange="valJpg(this);">
<script type="text/javascript">
valJpg.file = document.getElementById('myFile').cloneNode(true);
</script>
__________________
WWWWWWWWWWWW
- John
________________________

Really Show Your Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
Reply With Quote
Reply

Bookmarks

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

Forum Jump


All times are GMT. The time now is 02:42 AM.

Home - Contact Us - Archives - Link to DD - Top 

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.