|
#1
|
|||
|
|||
|
I have:
<input type="file" /> in my site with a submit button, How can I validate that this is a .jpg file? Thanks! |
|
#2
|
||||
|
||||
|
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>
Code:
onclick="checkJPG('i
Good luck.
__________________
|
| The Following User Says Thank You to Nile For This Useful Post: | ||
lord22 (11-07-2009) | ||
|
#3
|
||||
|
||||
|
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);">
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';
__________________
WWWWWWWWWWWW - John________________________ Really Show Your Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate |
| The Following User Says Thank You to jscheuer1 For This Useful Post: | ||
lord22 (11-07-2009) | ||
|
#4
|
|||
|
|||
|
Thank you both
|
|
#5
|
||||
|
||||
|
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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
|
|