当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Very confused about the proper way to use jquery validation

问题描述:

For some reason I can't quite grasp using jquery for form validation. Essentially all I'm looking for is to validate a form based on the rules I set (ie, name: "required" ) and when I hit a submit button, if those rules are not met, the form doesn't get submitted.

this is what I've come up with, but it's not working and I'm really lost. It will still submit the form even if the rules are not met. ie; if I don't fill in the project_title or country fields

$("#myform").validate({

rules:{

project_title: {

required:true

},

country: {

required:true

}

});

$("#myform").on('submit', function(e){

var isvalidate=$("#myform").valid();

if(isvalidate)

{

e.preventDefault();

alert(getvalues("myform"));

}

});

网友答案:
 $("#form1").validate({
     debug: false,
 rules: {
 project-title: "required",

 country: {
 required: true,

}
},
messages: {
project-title: "Please enter your project-title.",
country: "Please enter your country.",

},
submitHandler: function(form) {

 $.post('nextstep.php', $("#form1").serialize(), function(data) {
 $('#result').html(data);
});
}
});
});
</script>


 in html put 
<div id="result"></div>
网友答案:

Your rules object is malformed:

$("#myform").validate({
    rules:{
        project_title: {
            required:true
        },
        country: {
            required:true
        } 
    } // you're missing this closing curly brace
});

Working Fiddle.

分享给朋友:
您可能感兴趣的文章:
随机阅读: