Link to home
Start Free TrialLog in
Avatar of erikTsomik
erikTsomikFlag for United States of America

asked on

jquery validation validate onSubmit

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");
Avatar of Hagay Mandel
Hagay Mandel
Flag of Israel image

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

 $('#submit') .submit(function() {
  //Do the validation here:
}):
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

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...
Avatar of erikTsomik

ASKER

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();
Hi,

May be! Check that too!
I did . I just don't know how to overcome this.
Hi,

Just comment that code!
I can not because it is conditional depends on the value of server certain field will shown/hidden
Hi,

Can you post the full code? I'm not getting your problem exactly!
<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

ASKER CERTIFIED SOLUTION
Avatar of Roopesh Reddy
Roopesh Reddy
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial