Solved

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

Posted on 2010-09-03
5
496 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 500 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now