View Full Version : Validate Form Field

10-13-2005, 10:22 AM
I'm trying to validate a form file field, I need the user to insert a .jpg image, but if they leave the box blank then the field is filled out so that a default image is passed to the next page. I've got the .jpg thing working, but it's the other part I'm struggling with. Here's what I've got so far:

function validateform() {

alert("Please Select a JPG File");
return false;
else {
if (document.form1.file.value == "" ){
document.form1.file.value = 'no-image.jpg'

// Works out the last three characters of the file field
function Right(str, n)
if (n <= 0) // Invalid bound, return blank string
return "";
else if (n > String(str).length) // Invalid bound, return
return str; // entire string
else { // Valid bound, return appropriate substring
var iLen = String(str).length;
return String(str).substring(iLen, iLen - n);

<form enctype="multipart/form-data" method="POST" action="article_image.asp" onsubmit="return validateform();" id="form1" name="form1">
<p><input name="file" type="file" class="narcol" id="file" /></p>
<p>You can only upload image files with the extension .jpg. </p>
<p>If you do not have an image for this article, please leave the box blank and click next. </p>
<p><input name="Submit" type="submit" class="button" value="Next &gt;&gt;" />



10-13-2005, 08:08 PM
I'm not entirely sure what you'r problem is, but here's a possible solution: in the form's onSubmit... onSubmit="if (!validateForm()) { document.getElementById('file').value='default.jpg'; return false;}"