| | |
How to validate an image field with javascript
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
Are you sure you don't mean 'file' fields?
As for checking the file; you should be able to interogate the value of said file field, split it by slashes, and then split it by .:
(this WONT validate a form; it will just show you how to get the file extension from a filespec in string format. post back if you have any problems integrating this with existing code...)
Tested on Firefox and Opera.
As for checking the size of the file; this certainly can't be done with JS. You have to write some server side code to handle the upload correctly. That is probably a better way to check the filetype aswell; by interogating the MIME type of an uploaded file rather than just assuming the extension indicates filetype.
input type="image" fields are just graphical submit buttons; input type="file" fields let a user pick a file on their filesystem to upload..As for checking the file; you should be able to interogate the value of said file field, split it by slashes, and then split it by .:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<html> <head> <script type="text/javascript"> function getFileType(sValue) { var aParts = sValue.split( "/" ); var iParts = aParts.length; if( iParts >= 1 ) { var sFile = aParts[ iParts - 1 ]; var aFile = sFile.split( "." ); if( aFile.length == 2 ) { sName = aFile[0]; sExt = aFile[1]; alert("Filename is: "+sName+" extension is: "+sExt); } else { alert("Bad filespec; should have one dot between name and extension"); } } else { alert("No file selected"); } } </script> </head> <body> <form> <input type="file" id="file_input"/> <span onclick="getFileType(document.getElementById('file_input').value);">GetFileType</span> </form> </body> </html>
(this WONT validate a form; it will just show you how to get the file extension from a filespec in string format. post back if you have any problems integrating this with existing code...)
Tested on Firefox and Opera.
As for checking the size of the file; this certainly can't be done with JS. You have to write some server side code to handle the upload correctly. That is probably a better way to check the filetype aswell; by interogating the MIME type of an uploaded file rather than just assuming the extension indicates filetype.
Last edited by MattEvans; May 29th, 2007 at 10:02 am.
Plato forgot the nullahedron..
Just by validating the extension, you can't be sure that the file uploaded by the user is actually a image file. I can always rename an executable and make it seem like an image file, if your logic performs these kind of critical validations at the client side.
Better leave off these things to the server side validation. The size restriction as well as the type of file can only be meaningfully validated at the server. Doing it at the client side is a moot point.
Better leave off these things to the server side validation. The size restriction as well as the type of file can only be meaningfully validated at the server. Doing it at the client side is a moot point.
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- How to validate the textbox in datagrid using javascript? (ASP.NET)
- Javascript alert not working (JavaScript / DHTML / AJAX)
- calculate image property in javascript (JavaScript / DHTML / AJAX)
- case and image help please (PHP)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: How would I display somethign only if javascript is enabled?
- Next Thread: javascript function
| Thread Tools | Search this Thread |
ajax ajaxcode ajaxexample ajaxhelp ajaxjspservlets animate automatically beta box browser bug calendar captchaformproblem checkbox child class close column createrange() css cursor date debugger dependent disablefirebug dom download dropdown editor element embed engine error events explorer ext file form forms getselection google gwt gxt hiddenvalue highlightedword hint html htmlform ie7 ie8 iframe images internet java javascript javascripthelp2020 jawascriptruntimeerror jquery jsf jsfile jump libcurl math media microsoft mimic object onmouseoutdivproblem onreadystatechange parent paypal pdf php player position post problem programming progressbar regex runtime scroll search security select shopping size software sql text textarea unicode w3c web website window windowofwords windowsxp wysiwyg \n






