Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

jquery validation validate onSubmit

Posted on 2013-01-02
13
Medium Priority
?
1,422 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
[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
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Suggested Courses

610 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