form validation help

Results 1 to 6 of 6

Thread: form validation help

  1. #1
    Join Date
    Dec 1969
    Posts
    595

    Default form validation help

    I was wondering if anyone has or knows a (java)script to check a certain file extension in a form? I have a form that uploads graphic files and I want to allow only files w/ ext. .gif, .jpg etc... <BR>Thanks

  2. #2
    Join Date
    Dec 1969
    Posts
    96,118

    Default Should be easy enough...

    &#060;FORM ... onSubmit="return checkFile(this.TheFile);"&#062;<BR>&#060;INPUT Type=File Name="TheFile"&#062;<BR>...<BR>&#060;INPUT Type=Submit ...&#062;<BR>&#060;/FORM&#062;<BR><BR>&#060;SCRIPT&#062;<BR>function checkFile( fileField )<BR>{<BR> var fname = fileField.value;<BR> var ext = fname.substring( fname.lastIndexOf(".") + 1 );<BR> ext = ext.toLowerCase();<BR> if ( ext == "gif" &#124&#124 ext == "jpg" &#124&#124 ext == "tiff" ) return true;<BR> alert("Sorry, that doesn&#039;t appear to be a valid image file.");<BR> fileField.focus( );<BR> return false;<BR>}<BR>&#060;/SCRIPT&#062;<BR>

  3. #3
    Join Date
    Dec 1969
    Posts
    595

    Default why this doesn't work...

    function validation(frm){<BR> var fname = frm.file1.value; <BR> var ext = fname.substring( fname.lastIndexOf(".") + 1 ); <BR> ext = ext.toLowerCase(); <BR> if ( ext != "gif" &#124&#124 ext != "jpg" &#124&#124 ext != "tiff" ) {<BR> alert("Sorry, that doesn&#039;t appear to be a valid image file."); <BR> frm.file1.focus( ); <BR> return false; <BR>}<BR> }<BR>//--&#062;<BR>&#060;/script&#062; <BR><BR><BR>&#060;form name="frm" method="post" action="artwork.asp" onSubmit="return validation(this)" ENCTYPE="multipart/form-data"&#062;<BR>&#060;INPUT TYPE="FILE" NAME="file1" SIZE="20"&#062;<BR>&#060;/form

  4. #4
    Join Date
    Dec 1969
    Posts
    96,118

    Default You aren't doing...

    return true;<BR>from the validation function when it succeeds!!!<BR><BR>OH! And those should be && instead of &#124&#124 !!!<BR><BR>Here, this worked for me:<BR><BR>*****************<BR><BR>&#060;HTML&#06 2;&#060;BODY&#062;<BR>&#060;SCRIPT&#062;<BR><BR>fu nction validation(frm)<BR>{<BR> var fname = frm.file1.value; <BR> var ext = fname.substring( fname.lastIndexOf(".") + 1 ); <BR> ext = ext.toLowerCase(); <BR> if ( ext != "gif" && ext != "jpg" && ext != "tiff" ) <BR> {<BR> alert("Sorry, that doesn&#039;t appear to be a valid image file."); <BR> frm.file1.focus( ); <BR> return false; <BR> }<BR> return true;<BR>}<BR>//--&#062;<BR>&#060;/script&#062; <BR><BR><BR>&#060;form name="frm" method="post" action="artwork.asp" onSubmit="return validation(this)" <BR><BR>ENCTYPE="multipart/form-data"&#062;<BR>&#060;INPUT TYPE="FILE" NAME="file1" SIZE="20"&#062;<BR>&#060;INPUT Type=Submit&#062;<BR>&#060;/form&#062;<BR>&#060;/BODY&#062;&#060;/HTML&#062;<BR>


  5. #5
    Join Date
    Dec 1969
    Posts
    595

    Default Bill.. how about this...

    Thank you Bill, it works now.. but I have another question.<BR><BR>I have 3 files (file1, file2, file3) to check do I have to repeat this function 3 times?<BR><BR><BR>function validation(frm){<BR><BR>if (frm.file1.value != "") {<BR>var fname = frm.file1.value; <BR>var ext = fname.substring( fname.lastIndexOf(".") + 1 ); <BR>ext = ext.toLowerCase(); <BR>if ( ext != "gif" && ext != "jpg" && ext != "tiff" ) <BR>{<BR>alert("Sorry, that doesn&#039;t appear to be a valid image file."); <BR>frm.file1.focus( ); <BR>return false; <BR>}<BR>}<BR>if (frm.file2.value != "") {<BR>var fname2 = frm.file2.value; <BR>var ext = fname2.substring( fname2.lastIndexOf(".") + 1 ); <BR>ext2 = ext2.toLowerCase(); <BR>if ( ext2 != "gif" && ext2 != "jpg" && ext2 != "tiff" ) <BR>{<BR>alert("Sorry, that doesn&#039;t appear to be a valid image file."); <BR>frm.file2.focus( ); <BR>return false; <BR>}<BR>}<BR>if (frm.file3.value != "") {<BR>var fname3 = frm.file3.value; <BR>var ext3 = fname3.substring( fname3.lastIndexOf(".") + 1 ); <BR>ext3 = ext3.toLowerCase(); <BR>if ( ext3 != "gif" && ext3 != "jpg" && ext != "tiff" ) <BR>{<BR>alert("Sorry, that doesn&#039;t appear to be a valid image file."); <BR>frm.file3.focus( ); <BR>return false; <BR>}<BR>}<BR>return true;<BR>}<BR>//--&#062;<BR>&#060;/script&#062; <BR><BR>Thank you for your help!

  6. #6
    Join Date
    Dec 1969
    Posts
    96,118

    Default Learn to *USE* functions!

    &nbsp;<BR>Like this, perhaps:<BR><BR>*****************<BR><BR>function validation(frm)<BR>{<BR> return validateFile( frm.file1 ) <BR> && validateFile( frm.file2 ) <BR> && validateFile( frm.file3 );<BR>}<BR><BR>function validateFile(fld)<BR>{<BR> if (fld.value != "") {<BR> var fname = fld.value; <BR> var ext = fname.substring( fname.lastIndexOf(".") + 1 ); <BR> ext = ext.toLowerCase(); <BR> if ( ext != "gif" && ext != "jpg" && ext != "tiff" ) <BR> {<BR> alert("Sorry, " + fld.name + " doesn&#039;t appear to have a valid image file."); <BR> fld.focus( ); <BR> return false; <BR> }<BR> }<BR> return true;<BR>}<BR>

Posting Permissions

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