erikTsomik
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("fil ename", function(value, element) {
return this.optional(element) || /^.*\.(html|htm|swf)$/i.te st(value);
}, "Please specify a valid file name");
$.validator.addMethod("fil
return this.optional(element) || /^.*\.(html|htm|swf)$/i.te
}, "Please specify a valid file name");
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
}
}):
Hi,
Should be like this -
More info - http://docs.jquery.com/Plugins/Validation/Validator#Validator
Hope it helps u...
Should be like this -
$("#myform").validate({
submitHandler: function(form) {
form.submit();
}
});
More info - http://docs.jquery.com/Plugins/Validation/Validator#Validator
Hope it helps u...
ASKER
The problem is I the onchange event that what I think is triggering the validation as I start typing.
$('#server').change(functi on () {
if ($("#server").val() == 0) {
$("#file1").parent().show( );
$("#file2").parent().hide( );
$("#file2").val('');
}else{
$("#file1").parent().hide( );
$("#file2").parent().show( );
$("#file1").val('');
}
}).change();
$('#server').change(functi
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!
May be! Check that too!
ASKER
I did . I just don't know how to overcome this.
Hi,
Just comment that code!
Just comment that code!
ASKER
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!
Can you post the full code? I'm not getting your problem exactly!
ASKER
<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.'}
}
}
);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Suppose your submit button has an id of 'submit':
$('#submit') .submit(function() {
//Do the validation here:
}):