Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Apr 2007
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post error in javascript in form to check file extension

    Hi, actually there's no error in the code only that is only made to check for one input field and my form has 3 image upload fields, the original is:

    <form enctype="multipart/form-data" action="action.php" method="post" onSubmit="if(!this.img1.value.match(/\.jpg$/i)){alert('Must be JPG file');return false};return val(this)">
    <input type="file" name="img1">
    </form>
    This works great but what modifications should have to be able to check multiple file input fields?

    thanks a lot.

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    put it in a function and iterate through all your file-upload input elements

    Code:
    assuming u have a form like
    
    <form enctype="multipart/form-data" action="action.php" method="post" onSubmit="return validate(this)">
    <input type="file" name="img1" />
    <input type="file" name="img2" />
    ..
    <input type="file" name="img10" />
    </form>
    
    function validate(myForm) {
      var count = 10;
      var i;
      for ( i = 1; i <= count; i++ ) {
        if ( !/\.jpg/i.test(myForm['img' + i].value) {
          alert('error msg');
          return false;
        }
      }
      return true;
    }
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    If the input fields are named img1, img2, and img3, then you can use a loop.
    Code:
    function validate(frm){
      for (var i=1; i<=3; i++){
        if(!frm.elements["img"+i].value.match(/\.jpg$/i)){
          alert('Must be JPG file');
          return false;
        }
      }
      return val(this);
    }
    Code:
    <form enctype="multipart/form-data" action="action.php" method="post" onSubmit="return validate(this);">
    <input type="file" name="img1" />
    <input type="file" name="img2" />
    <input type="file" name="img3" />
    </form>
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #4
    New Coder
    Join Date
    Apr 2007
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts

    thanx

    thanx a lot, yes all my images were named in that order so the loop worked great, thanks to all.


  •  

    Posting Permissions

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