raman Posted October 7, 2009 Share Posted October 7, 2009 I have a form where I give the option to user to choose pasting the data in the textarea or upload it thorugh a file. Also I have two checkboxes, one or both of them must be filled. I have written a function which works for the textarea but not for <input type= file> field. Also how do I do it for checkboxes along with this condition such that the form will be submitted only when the user pates the data in the txet area or uploads the file and selects atleast one of the checkbox. Both the checkboxes have different names and different values in the form. function upload_validate() { var f1=document.getElementById('textarea content'); var f2=document.getElementById('userfile'); if((f1.value=='') && (f2.value=='')) { alert("Please insert protein sequences."); return false; } Quote Link to comment Share on other sites More sharing options...
eatfishy Posted October 7, 2009 Share Posted October 7, 2009 Wouldn't you rather use PHP server side script to check if a file name is empty or not so that it won't bug out on PHP? If someone disabled their javascript, it'll go through the PHP code you use to upload file. Quote Link to comment Share on other sites More sharing options...
raman Posted October 8, 2009 Author Share Posted October 8, 2009 I have already done it in my pHP script but I also want to add a client-side control, because in most cases Javascript is enabled. Why should it process the server-side PHP code if it's not required ? Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted October 8, 2009 Share Posted October 8, 2009 I have already done it in my pHP script but I also want to add a client-side control, because in most cases Javascript is enabled. Why should it process the server-side PHP code if it's not required ? The PHP check will be invoked anyway.... What does your HTML for the form look like? Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 8, 2009 Share Posted October 8, 2009 <html> <head> <script type="text/javascript"> var outTimes = new Array(); function validateForm(formObj) { var errors = new Array(); //Trim leading and trailing spaces formObj.tarea.value = formObj.tarea.value.replace(/^\s+|\s+$/g,''); //Validate the textarea if (!formObj.tarea.value) { errors[errors.length] = "Textarea is empty."; } //Validate the file upload if (!formObj.ffield.value) { errors[errors.length] = "A file must be selected."; } //Validate the checkboxes if (!formObj.cbox1.checked || !formObj.cbox2.checked) { errors[errors.length] = "At least one checkbox must be checked."; } //Process errors if (errors.length!==0) { var errMsg = 'The following errors ocured:\n'; for (var i=0; i<errors.length; i++) { errMsg += '\n - ' + errors[i]; } alert(errMsg); return false; } //No errors occured alert('No Errors'); return false; } </script> </head> <body> <form onsubmit="return validateForm(this)"> Textarea:<br /> <textarea name="tarea" id="tarea"></textarea> <br /><br /> File:<br /> <input type="file" name="ffield" id="ffield"> <br /><br /> Checkbox 1: <input type="checkbox" name="cbox1" id="cbox1" value="1"><br /> Checkbox 2: <input type="checkbox" name="cbox2" id="cbox2" value="2"><br /> <br /><br /> <button type="submit">Submit Form</button> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.