Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

856 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