Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    May 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    alert the user of wrong answers

    I am new to Javascript. I am working for a medical research firm. I have a form which has two criteria's in it. One is INCLUSION criteria(which has 10 questions with two(yes/no) checkboxes and the other is EXCLUSION criteria(which has 20 questions with two(yes/no) checkboxes) and I have a save button at the bottom.
    When the user answers these questions and clicks the save button, I need to check the values of all the 30 questions and alert the user with the msg "All Inclusions should be answered YES and exclusions should be answered NO" along with the questions that were answered wrong.
    Can someone please help me with this. I need this urgently.
    Thanks a lot in advance.

  • #2
    Regular Coder Nischumacher's Avatar
    Join Date
    Oct 2005
    Location
    Bombay, India
    Posts
    196
    Thanks
    0
    Thanked 2 Times in 2 Posts
    iF aLL THe iNCLuSioNS HaVe To Be YeS... aND aLL THe eXCLuSioNS HaVe To Be No... THeN WHY GiVe THe CHeCHBoXeS ?
    SiMPLY LiST THeM aND aSK iF iT aCCePTaBLe... PRoCeeD oN SuBMiT.

    aNY WaY... HeRe PRoBaBLY iS WHaT You WaNT...
    aND i HaVe TaKeN THe LiBeRTY To CHaNGe THe CHeCKBoXeS To RaDioS...
    Code:
    <html>
    <head>
    <script type="text/javascript">
    var flag1=true;
    var flag2=true;
    var msg;
    function onSubFunc() {
    	msg="";
    	for (var i=1; i<=3; i++) {
    		if (document.getElementsByName('inc'+i)[0].checked==false) {
    			msg += 'Q' + i + '. should be YES\n';
    			flag1 = false;
    		}
    	}
    	for (var i=4; i<=9; i++) {
    		if (document.getElementsByName('exc'+i)[1].checked==false) {
    			msg += 'Q' + i + '. should be NO\n';
    			flag2 = false;
    		}
    	}
    	if (!flag1 || !falg2) {
    		alert('All Inclusions should be answered YES\nand exclusions should be answered NO.\n\nThe wrong selections are listed below:\n' + msg);
    		return false;
    	}
    }
    </script>
    </head>
    <body>
    <form method="post" onSubmit="return onSubFunc();">
    INCLUSIONS :<br>
    Q1. <input type="radio" name="inc1" value="Yes">Yes <input type="radio" name="inc1" value="No">No<br>
    Q2. <input type="radio" name="inc2" value="Yes">Yes <input type="radio" name="inc2" value="No">No<br>
    Q3. <input type="radio" name="inc3" value="Yes">Yes <input type="radio" name="inc3" value="No">No<br>
    <br>
    EXCLUSIONS :<br>
    Q4. <input type="radio" name="exc4" value="Yes">Yes <input type="radio" name="exc4" value="No">No<br>
    Q5. <input type="radio" name="exc5" value="Yes">Yes <input type="radio" name="exc5" value="No">No<br>
    Q6. <input type="radio" name="exc6" value="Yes">Yes <input type="radio" name="exc6" value="No">No<br>
    Q7. <input type="radio" name="exc7" value="Yes">Yes <input type="radio" name="exc7" value="No">No<br>
    Q8. <input type="radio" name="exc8" value="Yes">Yes <input type="radio" name="exc8" value="No">No<br>
    Q9. <input type="radio" name="exc9" value="Yes">Yes <input type="radio" name="exc9" value="No">No<br>
    <br>
    <input type="submit">
    </form>
    </body>
    </html>
    - NS 666
    .net DEVILoper

  • #3
    New to the CF scene
    Join Date
    May 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you so much for the reply. My problem is I should give the whole Question in the alert msg. I will make myself clear. Suppose I have a question "Does the participant have the presence of sub-retinal hemorrhage larger than 1 disc area?". This should be answered "No". But if it is checked as "Yes", when the user clicks the save button I should give a msg " All Inclusions should be answerd YES and Exclsions should be answered NO" and then the question "Does the participant have the presence of sub-retinal hemorrhage larger than 1 disc area?" is wrong.

    I should be able to do this with all the questions.

  • #4
    Regular Coder Nischumacher's Avatar
    Join Date
    Oct 2005
    Location
    Bombay, India
    Posts
    196
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Code:
    <html>
    <head>
    <script type="text/javascript">
    var flag = true;
    var msg;
    function onSubFunc() {
    	msg="";
    	for (var i=1; i<=3; i++) {
    		if (document.getElementsByName('rad'+i)[0].checked==false) {
    			msg += document.getElementById('q'+i).innerText + '\n';
    			flag = false;
    		}
    	}
    	for (var i=4; i<=6; i++) {
    		if (document.getElementsByName('rad'+i)[1].checked==false) {
    			msg += document.getElementById('q'+i).innerText + '\n';
    			flag = false;
    		}
    	}
    	if (!flag) {
    		alert('All Inclusions should be answered YES\nand exclusions should be 
    
    answered NO.\n\nYou answered inccorectly to the following question(s) :\n' + msg);
    		return false;
    	}
    }
    </script>
    </head>
    <body>
    <form method="post" onSubmit="return onSubFunc();">
    INCLUSIONS :<br>
    <label id="q1">Q1. the first question ?</label><br>
    <input type="radio" name="rad1" value="Yes">Yes <input type="radio" name="rad1" 
    
    value="No">No<br>
    <label id="q2">Q2. the second question ?</label><br>
    <input type="radio" name="rad2" value="Yes">Yes <input type="radio" name="rad2" 
    
    value="No">No<br>
    <label id="q3">Q3. the third question ?</label><br>
    <input type="radio" name="rad3" value="Yes">Yes <input type="radio" name="rad3" 
    
    value="No">No<br><br>
    INCLUSIONS :<br>
    <label id="q4">Q4. the fourth question ?</label><br>
    <input type="radio" name="rad4" value="Yes">Yes <input type="radio" name="rad4" 
    
    value="No">No<br>
    <label id="q5">Q5. the fifth question ?</label><br>
    <input type="radio" name="rad5" value="Yes">Yes <input type="radio" name="rad5" 
    
    value="No">No<br>
    <label id="q6">Q6. the sixth question ?</label><br>
    <input type="radio" name="rad6" value="Yes">Yes <input type="radio" name="rad6" 
    
    value="No">No<br><br>
    <input type="submit">
    </form>
    </body>
    </html>
    - NS 666
    .net DEVILoper

  • #5
    New to the CF scene
    Join Date
    May 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very much.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •