Solved

jquery validation validate onSubmit

Posted on 2013-01-02
13
1,340 Views
Last Modified: 2013-01-23
I have a custom validation to validate on submit I've added a function to do that but it is also validate as I type. How can i change it so it is only validate when i click the submit button

$.validator.addMethod("filename", function(value, element) {
             
                         return this.optional(element) || /^.*\.(html|htm|swf)$/i.test(value);
            }, "Please specify a valid file name");
0
Comment
Question by:erikTsomik
13 Comments
 
LVL 16

Expert Comment

by:HagayMandel
ID: 38739090
Insert the code in a submit event:
Suppose your submit button has an id of 'submit':

 $('#submit') .submit(function() {
  //Do the validation here:
}):
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 38739124
0
 
LVL 11

Expert Comment

by:mcnute
ID: 38739462
if you put validation into submit be sure to add e.preventDefault(); to it. Otherwise it just submits as supposed without validating.

$('#submit') .click(function(e) {
e.preventDefault(); // prevents from submitting

// do validation here

if(valid) {
$(this).submit();

} else {
// show error msg somewehre
}
}): 

Open in new window

0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 23

Expert Comment

by:Roopesh Reddy
ID: 38740323
Hi,

Should be like this -

$("#myform").validate({
 submitHandler: function(form) {
   form.submit();
 }
});

Open in new window


More info - http://docs.jquery.com/Plugins/Validation/Validator#Validator

Hope it helps u...
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 38740395
The problem is I the onchange event that what I think is triggering the validation as I start typing.
$('#server').change(function () {
                        if ($("#server").val() == 0) {
                              $("#file1").parent().show();
                              $("#file2").parent().hide();
                              $("#file2").val('');
                        }else{
                              $("#file1").parent().hide();
                              $("#file2").parent().show();
                              $("#file1").val('');
                        }
                  }).change();
0
 
LVL 23

Expert Comment

by:Roopesh Reddy
ID: 38740533
Hi,

May be! Check that too!
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 38740560
I did . I just don't know how to overcome this.
0
 
LVL 23

Expert Comment

by:Roopesh Reddy
ID: 38740622
Hi,

Just comment that code!
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 38740634
I can not because it is conditional depends on the value of server certain field will shown/hidden
0
 
LVL 23

Expert Comment

by:Roopesh Reddy
ID: 38740656
Hi,

Can you post the full code? I'm not getting your problem exactly!
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 38740678
<script>
$(document).ready(function(){
			$.validator.addMethod("filename", function(value, element) {
               // return this.optional(element) || /^[a-z]\w*\.(html|htm|swf)$/i.test(value);
				 return this.optional(element) || /^.*\.(html|htm|swf)$/i.test(value);
            }, "Please specify a valid file name");



});


	$(document).ready(function () {
		//alert($("#locOwner").val());
		$("#audienceName").parent().show();
		if ($("#locOwner").val() == 0){
			$("#creator").parent().show();
			$("#tool").parent().show();
			$("#server").parent().show();
			$("#file1").parent().hide();
			$("#file2").parent().hide();
			$('#server').change(function () {
				if ($("#server").val() == 0) {
					$("#file1").parent().show();
					$("#file2").parent().hide();
					$("#file2").val('');
				}else{
					$("#file1").parent().hide();
					$("#file2").parent().show();
					$("#file1").val('');
				}
			}).change();
		}else{
			$("#creator").parent().hide();
			$("#tool").parent().hide();
			$("#server").parent().hide();
			$("#file1").parent().hide();
			$("#file2").parent().hide();
		}
	});
</script>

$('#audienceForm').validate(
{
rules: {
creator: {
required: function(element) {
return ($("#locOwner").val()== 0);
}
},
tool: {
required: function(element) {
return ($("#locOwner").val()== 0);
}
},
server: {
required: function(element) {
return ($("#locOwner").val()== 0);
}
},
audienceName:{
required: function(element){
return ($("#adminAudience").val().length == 0);
}
},
file1: {
required: function(element) {
return ($("#locOwner").val()== 0 && $("#server").val()== 0);
}
,
filename: true
},
file2: {
required: function(element) {
return ($("#locOwner").val()== 0 && $("#server").val()== 1);
},
url:true
}
},
messages:{
audienceName:{ required: 'Enter a Administrator Audience.'}
}
}
);

Open in new window

0
 
LVL 23

Accepted Solution

by:
Roopesh Reddy earned 500 total points
ID: 38742940
Hi,

Why two $(document).ready() functions?

Can you also post the HTML, so that we can test at our end and assist you better?

OR
create a simple stuff for us to check and we will assist to you on that!
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
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…

679 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