Solved

parsley js

Posted on 2014-09-14
7
985 Views
Last Modified: 2014-09-21
Hi,

i have a group of five cells and i want to use parsley js to validate the form. Essentially i want one of the five cells to be filled out. if not the throw the warning if one or more if filled out them the user can submit the form

i found this article but it is comparing two groups
http://parsleyjs.org/doc/examples/events.html

can someone hep me how to do this

thanks
0
Comment
Question by:M. Jayme Nagy
  • 3
  • 3
7 Comments
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 40322460
<head>
    <title></title>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#demo-form').parsley().subscribe('parsley:form:validate', function (formInstance) {

                // if one of these blocks is not failing do not prevent submission
                // we use here group validation with option force (validate even non required fields)
                if (formInstance.isValid('block1', true) || formInstance.isValid('block2', true) || formInstance.isValid('block3', true) || formInstance.isValid('block4', true) || formInstance.isValid('block5', true)) {
                    $('.invalid-form-error-message').html('');
                    return;
                }
                // else stop form submission
                formInstance.submitEvent.preventDefault();

                // and display a gentle message
                $('.invalid-form-error-message')
      .html("You must correctly fill the fields of at least one of these five blocks!")
      .addClass("filled");
                return;
            });
        });
</script>
</head>
<body>
<form id="demo-form" data-parsley-validate>
  <div class="first">    
    <input type="text" name="name1" data-parsley-range="[4, 20]" data-parsley-group="block1" />
  </div>  
  <div class="second">    
    <input type="text" name="name2" data-parsley-range="[8, 40]" data-parsley-group="block2" />
  </div>
  <div class="third">    
    <input type="text" name="name3" data-parsley-range="[4, 20]" data-parsley-group="block3" />  
  </div> 
  <div class="fourth">    
    <input type="text" name="name4" data-parsley-range="[8, 40]" data-parsley-group="block4" />
  </div>
  <div class="fifth">    
    <input type="text" name="name5" data-parsley-range="[8, 40]" data-parsley-group="block5" />
  </div>


  <div class="invalid-form-error-message"></div>
  <input type="submit" class="btn btn-default validate" />
</form>
</body>

Open in new window

0
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
ID: 40322492
Retaining the use of validating the one block, you can do it utilising the data-parsley-validate-if-empty attribute and binding to the form-validation event:

http://jsbin.com/hiciku/5/

$(function() {

	$('#demo-form').parsley().subscribe('parsley:form:validate', function (formInstance) {


		var flag = false;
		$.each($("input[data-parsley-group=block1]"),function(i,el) {
			if (el.value.length > 0) {
				$('.invalid-form-error-message').html('');
				flag = true;
				return false;
			}
		});

		if (flag) {return;}
		// else stop form submission
		formInstance.submitEvent.preventDefault();

		// and display a gentle message
		$('.invalid-form-error-message')
		.html("You must correctly fill the fields of at least one of these two blocks!")
		.addClass("filled");
		return;
	});

});

Open in new window


<!DOCTYPE html>
<html>
	<head>
		<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
		<script src="http://parsleyjs.org/dist/parsley.min.js"></script>
		<meta charset="utf-8">
		<title>JS Bin</title>
	</head>
	<body>
		<form id="demo-form" data-parsley-validate>
			<div class="first">
				<label for="firstname">Field 1:</label>
				<input type="text" name="field1" data-parsley-group="block1" data-parsley-validate-if-empty />

				<label for="lastname">Field 2:</label>
				<input type="text" name="field2" data-parsley-group="block1" data-parsley-validate-if-empty/>

				<label for="lastname">Field 3:</label>
				<input type="text" name="field3" data-parsley-group="block1" data-parsley-validate-if-empty/>

				<label for="lastname">Field 4:</label>
				<input type="text" name="field4" data-parsley-group="block1" data-parsley-validate-if-empty/>

				<label for="lastname">Field 5:</label>
				<input type="text" name="field5" data-parsley-group="block1" data-parsley-validate-if-empty/>

			</div>
			<div class="invalid-form-error-message"></div>
			<input type="submit" class="btn btn-default validate" />
		</form>
	</body>
</html>

Open in new window

0
 
LVL 6

Author Comment

by:M. Jayme Nagy
ID: 40323343
Hi,

is there any way to do it with an inline company such as the one you used for the group?
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 6

Author Comment

by:M. Jayme Nagy
ID: 40323351
Additionally, can we adjust the function to fire on change / keydown
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 40323729
is there any way to do it with an inline company such as the one you used for the group?
By inline company do you mean data-parsley-company

I'll have a look about firing on keychange
0
 
LVL 6

Author Comment

by:M. Jayme Nagy
ID: 40323762
yea i believe that is what i mean

thanks
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 40323840
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

803 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