使用Jquery验证验证表单(Validate form with Jquery validation)

编程入门 行业动态 更新时间:2024-10-28 04:29:58
使用Jquery验证验证表单(Validate form with Jquery validation)

我想验证我的表单,但我有这个错误:

Uncaught TypeError: $(...).validate is not a function

我包含了js文件验证jquery-validate.min.js和jquery-validate.js.Thanks

的index.html

<form id="form1"> <label for="phone">First Name</label> <input type="text" name="name" required /> <input type="button" id="btn" value="ok" /> </form>

file.js

$(document).ready(function (){ $("#form1").validate({ rules: { name: "required" }, messages: { name: "Please specify your name" } }) $('#btn').click(function() { $("#form1").valid(); }); });

I want to validate my form, but I had this error :

Uncaught TypeError: $(...).validate is not a function

I included the js files validation jquery-validate.min.js and jquery-validate.js.Thanks

index.html

<form id="form1"> <label for="phone">First Name</label> <input type="text" name="name" required /> <input type="button" id="btn" value="ok" /> </form>

file.js

$(document).ready(function (){ $("#form1").validate({ rules: { name: "required" }, messages: { name: "Please specify your name" } }) $('#btn').click(function() { $("#form1").valid(); }); });

最满意答案

检查一下并注意脚本的包含方式:

$(document).ready(function (){    
    $("#form1").validate({
        rules: {
            name: "required"
        },
        messages: {
            name: "Please specify your name"
        }
    });
    
    $('#btn').click(function() {
        $("#form1").valid();
    });
}); 
  
#form1 label.error {
    color: #FB3A3A;
    display: block;
    margin: 10px;
    padding: 0;
    text-align: left;
} 
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/jquery.validate.min.js"></script>

<form id="form1">
<label for="name">First Name</label>
<input type="text" name="name" required />
<input type="button" id="btn" value="ok" />
</form> 
  
 

Check this and note how the scripts are included:

$(document).ready(function (){    
    $("#form1").validate({
        rules: {
            name: "required"
        },
        messages: {
            name: "Please specify your name"
        }
    });
    
    $('#btn').click(function() {
        $("#form1").valid();
    });
}); 
  
#form1 label.error {
    color: #FB3A3A;
    display: block;
    margin: 10px;
    padding: 0;
    text-align: left;
} 
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/jquery.validate.min.js"></script>

<form id="form1">
<label for="name">First Name</label>
<input type="text" name="name" required />
<input type="button" id="btn" value="ok" />
</form> 
  
 

更多推荐

本文发布于:2023-07-22 06:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1217747.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   Jquery   Validate   validation   form

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!