SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Input array extension validation

    Hi everybody
    I use this function I found in SP:

    HTML Code:
    <script language="JavaScript" type="text/javascript">
    function checkform ( f ) {
    	var frm0 = f.elements['uploadfile[0]'].value;
    	var frm1 = f.elements['uploadfile[1]'].value;
    	var frm2 = f.elements['uploadfile[2]'].value;
    	if ( frm0 == "" && frm1 == "" && frm2 == "" ) {
            alert("Post file must not be left blank");
    		return false;
    	}
    	return true;
    }
    </script>
    
    <form enctype="multipart/form-data" action="upload.php" method="post" onsubmit="return checkform(this);">
    
    <input type="file" name="uploadfile[0]">
    <input type="file" name="uploadfile[1]">
    <input type="file" name="uploadfile[2]">
    <input type="submit" value=" Upload File(s) " /> &nbsp;
    </form>
    I need add there extension validation. Per sample uploadfile[0] must be .doc, uploadfile[1] must be .mp3 and uploadfile[2] must be .avi
    I can't get the solution. Somebody can help me ? Thanks.

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should check the file extension server-side (PHP, Java, etc). You will have enough information there (as opposed to the client-side) to perform a more thorough and secure validation.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, you right of course but I need check extension before uploading. On uploading process I checking again but I need limit file type in that input box.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    You can use the slice function to check the end of the filename.

    For example:

    Code javascript:
    if ( frm0.slice(-4) !== '.doc') {
        alert("Post file must be a .doc file");
        return false;
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great, thank you !


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •