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>
The standard method to find elements by ID is .getElementById()
:
document.getElementById("file-upload").focus();
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.
why don't you try validate jQuery plugin? take a look at here :
http://bassistance.de/jquery-plugins/jquery-plugin-validation/
精彩评论