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
  • 60,866
  • 25
  • 183
  • 186
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
  • 142,137
  • 41
  • 261
  • 360
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
  • 12,234
  • 11
  • 45
  • 68
0

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

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

hd.
  • 17,596
  • 46
  • 115
  • 165