Solved

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

Posted on 2010-09-03
5
518 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Suggested Solutions

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective 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…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

752 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