Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate input from FILE input field?

Status
Not open for further replies.

youradds

Programmer
Joined
Jun 27, 2001
Messages
817
Location
GB
Hi,

I have the following code;

Code:
if (document.p.FILE1.value == "") {
  alert('You need to specify a FILE for document number 1');
 return false;
}

(repeated 10 times, for 10 different "FILExx" fields).

However, even with ALL of them populated with content, it shows the error message for number 3 (FILE3).

Is "document.p.FILE1.value" the correct string format to use? As I said, it works fine for 1 and 2, but stops at 3 :/

Also - while I'm here :D Does anyone have a good regex code to verify the files are .rtf/.txt/.doc format? We check via a Perl script anyway, but it saves the users a lot of hassle if done before submitting to the script (i.e going back a page, "clears" out the "file" fields, which is a bit of a PITA :()

TIA!

Andy
 
Try it like in my example below. It will test all of the file fields, just name them sequentially, add ID fields to each of the file fields and in the function change the number of the for loop to be one higher than the number of file fields you have on the page.

Code:
<script type="text/javascript">
function chkname(fldNum) 
{ 
  var regExp=/\.rtf|.txt|.doc$/; 
  for (var x=1; x<4; x++) {
    if (document.getElementById('file'+x).value != '') {
      if (!regExp.test(document.getElementById('file'+x).value)) { 
        alert('Invalid file selection in selection field ' + x + '\n\r' + "You must select an rtf, txt or doc file."); 
        return false;
      }
    }
    else {
      alert('No file was selected in selection field ' + x);
      return false;
    }
  }
  return true;
}
</script>
<form name="text" method="post" action="" onsubmit="return chkname();">
<input type="file" id="file1"><br>
<input type="file" id="file2"><br>
<input type="file" id="file3"><br>
<input type="Submit" value="Submit">

It's hard to think outside the box when I'm trapped in a cubicle.
 
You can also name all of the file fields the same and treat them as an array then you do not have to set the number in the for loop.
Code:
<script type="text/javascript">
function chkname(fldNum) 
{ 
  var regExp=/\.rtf|.txt|.doc$/; 
  var arrFiles = document.getElementsByName('file');
  for (var x=0; x<arrFiles.length; x++) {
    if (arrFiles[x].value != '') {
      if (!regExp.test(arrFiles[x].value)) { 
        alert('Invalid file selection in selection field ' + (x+1) + '\n\r' + "You must select an rtf, txt or doc file."); 
        return false;
      }
    }
    else {
      alert('No file was selected in selection field ' + (x+1));
      return false;
    }
  }
  return true;
}
</script>
<form name="text" method="post" action="" onsubmit="return chkname();">
<input type="file" id="file"><br>
<input type="file" id="file"><br>
<input type="file" id="file"><br>
<input type="Submit" value="Submit">
<br>
</form>

I do not know if you will have problems with your upload application addressing the files referenced this way though.


It's hard to think outside the box when I'm trapped in a cubicle.
 
Hi,

Thanks for the replys guys. I found out why my old code wasn't working :P It was a stupid mistake on my end! The error message wasn't actually refering to the FILE3 field, but the NumberOfWords3 one ;) (I had neglected to edit the error message, from a copy+paste).

I couldn't get your code to work theniteowl (thanks anyway!).

I ended up using bits of your stuff;

Code:
 if (document.p.FILE1.value == "") {
   alert('You need to specify a FILE for document number 1');
   return false;
 } else {
   var regExp=/\.rtf|.txt|.doc$/;
   if (!regExp.test(FILE1.value)) {
    alert('Invalid FILE specified for document number 1. Accepted formats are: .doc, .txt and .rtf');
   }
 }

Thanks for the replys guys :)

Cheers

Andy
 
It worked fine at least in IE when I tested here. You probably did not set ID tags for your file fields, or did not notice that I used a lowercase name for file where you had uppercase.

No big deal, what you really needed was the regExp but my code simplified both checks into a single loop rather than repeating code over and over. It's just cleaner and does not require a lot of updating anytime you add/remove fields.


It's hard to think outside the box when I'm trapped in a cubicle.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top