Solved

jquery validation validate onSubmit

Posted on 2013-01-02
13
1,324 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
When we use cache:false ajax? 1 33
where is team.js? 3 25
Javascript Form Change 5 18
tiny glitch in my main slider 3 23
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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…

770 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