0

Modified question so you can stop taking the michael!

Ok i want use jquery to validate it.

The form values are from hidden inputs. So if any hidden inputs == 0, i want to alert the user to complete the form. If all == 1 i want the form to be submitted.

I want to submit via input button type with an onclick event.

<script>
jquery function
if all hidden == 0
alert
else 
location.href='submit2pd.php
</script>
<input onclick="(the jquery function)" type="button" value="Submit" />
6
  • Woah! That's some uber code! Are the values actually 0 or empty ("")? Could you post your HTML for the form? Commented Oct 13, 2011 at 15:12
  • Why isn't it working? Do you see an error message? How does it fail to perform as you expect? Commented Oct 13, 2011 at 15:13
  • You need to submit the form if it validates, at the moment you will just redirect the user to a PHP and wipe out what they have put in the form fields. Commented Oct 13, 2011 at 15:14
  • @Alex: html/php code is huge, basically i want an error message if any of the 31 inputs are 0 (values are 0). Im using a jquery autosave script on form submit button so thats why i want a seperate button to submit to other script, this saves to db and emails a notification. Commented Oct 13, 2011 at 15:17
  • @maerics: i click the button and it does nothing Commented Oct 13, 2011 at 15:18

5 Answers 5

2

Removing the entire else block will probably do the trick (if your <form> has a target). As for the rest of your code, well... Never mind.

Sign up to request clarification or add additional context in comments.

Comments

2

Do yourself a favor and learn some jQuery (or other js library of your choice). You can replace your function to something like this:

$('#formId').submit(function(e){
    $(this).find('checkbox').each(function(){
        if($(this).val() == '0') {
            alert('Please complete the form before submitting.');
            e.preventDefault();   
        }  
    });
});

2 Comments

I have learnt quite a bit of jquery recently and this form is already using alot of it. I tried using jquery validation but could not get it working, thought it would be easier doing it this way.
As i have the autosave function on form submit i want to keep the validation separate. I want the user to be able to save the form without it validating so they can come back at a later date and then submit it.
1

An example of what you want to do that works can be found here: http://www.shiningstar.net/articles/articles/javascript/checkboxes.asp

I'll give you a specific answer and a more conceptual answer about why what you have doesn't work.

Specifically, you want to replace all those items with checklist.whatever.checked==false

More conceptually, that long chain of or statements is not good software engineering and you will make yourself miserable maintaining that. You need to use a for loop to make it work because it is just too easy to introduce typos into that code.

I agree with Andre that you should use jQuery, because it's API for interacting with the HTML elements is easier than the native JavaScript API. Second, get firebug and learn to use console.log. Third go to the JavascriptLint site and learn how to use that tool.

Comments

0

In your function checklist is the input element so checklist.taccept would not find an element. You need to pass through your form as checklist in order for this to work.

I think the jQuery solution by Andre is a lot more elegant and just as easy to implement. Instead of the submit handler, just add that as a click even to your button and change $(this) to $("#formid")

Comments

0

Try sticking in an alert('in function...bla'); or two in that jscript block, then you can tell that its being called successfully, then you can deal with what you are trying to do in the block.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.