Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Does anyone know how to get Validity javascript plugin to work with a radio button?

Posted on 2010-09-03
5
Medium Priority
?
536 Views
Last Modified: 2012-05-10
I just found this Validity plugin:

http://validity.thatscaptaintoyou.com/

which states it does conditional validation but I cant figure out what the syntax is to get it to work with a radio button?

Can someone tell me if it does do conditional validation with a radio button and if so please give me an example on the net to follow.

Also If validity does not do conditional validation with radio buttons can you please point me in the right direction?

Thanks!
0
Comment
Question by:HudsonMarine
  • 2
  • 2
5 Comments
 
LVL 22

Expert Comment

by:kadaba
ID: 33597966
http://validity.thatscaptaintoyou.com/Demos/index.htm

Depends on what kind of conditional validation you need to do on the radios.
0
 
LVL 1

Author Comment

by:HudsonMarine
ID: 33598808
where at least one radio button is required and one out of several check boxes are required.

I cant find a definite javascript plugin that will validate based on this criteria.
0
 
LVL 22

Accepted Solution

by:
kadaba earned 2000 total points
ID: 33599565
I put up a simple example using the documentation. For radio buttons make one of them default selected so that will avoid the validation. If not follow the same code, just replace the checkbox with radio. They will work the same way.

<html>
    <head>
        <title>Conditional</title>
        <link rel="Stylesheet" type="text/css" href="jquery.validity.css" />
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript" src="jquery.validity.js"></script>
        <script type="text/javascript">
            $(function() { 
                $("form").validity(function() 
				{ 
					var fields = $("input[name='maillist']").serializeArray();
					var isNotChecked = true;
					
					if (fields.length == 0) 
					{
						isNotChecked = false;
					}
					
                   $("#choice")
						.assert(
							(isNotChecked), 
							"Please select at least one choice"
						);

                });
            });
        </script>
    </head>
    <body>
        <form action="conditional.htm" method="get">	
			<div id="choice">
			</div>
			<div>
			Choice1<input type="checkbox" id="maillist1" name="maillist" /><br>
			Choice2<input type="checkbox" id="maillist2" name="maillist" /><br>
			Choice3<input type="checkbox" id="maillist3" name="maillist" /><br>
			Choice4<input type="checkbox" id="maillist4" name="maillist" /><br>
			</div>
            <input type="submit" value="Submit" />
        </form>
    </body>
</html>

Open in new window

0
 
LVL 1

Author Comment

by:HudsonMarine
ID: 33640053
your example works when validating only the radio button,  but I am having trouble incorporating this with multiple conditional validations which is the ultimate goal.    With the script below I see that the validation is running on submit (alert messages appear on the screen) but then the form continues the submit operation without stopping. Please advise where my syntax has gone wrong.
$(function() 
	{ 
        $("form").validity(function() 
		 { 
			
			 if ($("#_2B_Industrial_Operations").attr("checked")) 
				{
					var fields = $("input[name='_2B_IO']").serializeArray();
					var isNotChecked = true;
							
							if (fields.length == 0) 
							{
								isNotChecked = false;
							}
							
						  $("#choice")
								.assert(
									(isNotChecked), 
									"Please make a selection");
							
					if ($("#_2B_IO_3").attr("checked")) 
						{
			
						$("#_2B_IO_Other_field")
						.require()          
						.maxLength(50);
						
						}			
				}
				
			if ($("#_2C_Ballast_Water_Treatment").attr("checked")) 
						{
							$("#_2C_BWT_Derivitives")
								.require("Value is required.")
								.maxlength(50);
						}
        });
    });

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question