0

i have a form which i want to be validated with javascript before submitting. What i am trying is adding a filed to upload file with an id having a dash in its value eg id="file-upload". This doesn't work however if i remove the dash from the id ... the alert works fine..

here is the javascript code

<script type="text/javascript">function check(form) { if(form.file-upload.value===""){alert("Please select a file to upload !");form.file-upload.focus();return (false);} } </script>

and here is the form

<form method="post" action="" onSubmit="return check(this)" enctype="multipart/form-data">
     <label>Upload Image:<span class="asterixRequired">*</span></label>
    <fieldset><input type="file" id="file-upload" name="file-upload"></fieldset> 
                                   </form>
jub0bs
  • 54,300
  • 24
  • 162
  • 166
Harjeet Singh
  • 25
  • 3
  • 6

3 Answers3

1

The standard method to find elements by ID is .getElementById():

document.getElementById("file-upload").focus();
Álvaro González
  • 135,557
  • 38
  • 250
  • 339
0

Check out Javascript and CSS, using dashes as well as this question/solution:
http://www.aspmessageboard.com/showthread.php?t=125142

Obviously JavaScript has problems with the dash, so you may have to use
document.forms['formname'].elements['field-with-dashes'].value
(from the above link) to solve this.

Community
  • 1
  • 1
Select0r
  • 11,934
  • 11
  • 43
  • 64
0

why don't you try validate jQuery plugin? take a look at here :

http://bassistance.de/jquery-plugins/jquery-plugin-validation/

hd.
  • 16,676
  • 43
  • 110
  • 160