Networking | Hardware | Software | Multimedia | System | Unix&Linux | MBA

Home>>Hardware>>Validate Form Field

Validate Form Field

djfenom
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() {

if(Right(document.form1.file.value,3)!="jpg"){
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;" />
</p>
</form>

Thanks

Chris

Pyth007
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;}"


 

TOP

For more info

setting a minimum size
Auto Preview of Image 
Refresh with a query s
JS Validation obsolete
Javascript Pop up func
Slide show transitions
I'm a newbie...please!
how to write coding in
Advanced Debug Info 
.js file doesn't work 

News Archive

Adding Graphic To Text
re-direct form after s
pop up window controls
Clock on homepage - He
passing a param from a
Password Procection wi
document.title in same
focus onload 
Counting Wrong from Ri
Help needed on how to 

Related stories:

Carriage Return FOR Javascript Code
Modifier key recognised with an ahref/onClick event?
getElementById won't work on Personal Computer
count subfolders and pass on values of array length to other page?
autocomplete question
just some thoughts about new Date() object
How can i write javascript for server controls?
Dynamically creating variables.

Copyright@2004-2005 www.zzcoke.com All Right Reserved

advanced web statistics