jquery.validate使用時遇到的問題

來源:互聯網
上載者:User

jquery.validate使用時遇到的問題

   這篇文章主要介紹了jquery.validate使用時遇到的問題的解決方案的匯總,真是應了那句老話,會者不難難者不會,這裡記錄一下,有需要的小夥伴可以參考下。

  問題一:

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

<script src="../js/jquery.js"></script>

<script src="../js/jquery.validate.js"></script>

<script>

$().ready(function() {

$("#registerForm").validate();

});

</script>

 

<form id="registerForm" method="get" action="">

<fieldset>

<p>

<label for="cusername">使用者名稱</label>

<input id="cusername" name="username" type="text" data-rule-required="true" data-rule-rangelength="[2,10]" data-msg-required="使用者名稱不可為空" data-msg-rangelength="使用者名稱長度必須是2到10個字元">

</p>

<p>

<label for="cpassword">密碼</label>

<input id="cpassword" name="password" type="password" data-rule-required="true" data-rule-minlength="6" data-msg-required="密碼不可為空" data-msg-minlength="至少設定6位密碼">

</p>

<p>

<label for="cconfirmpassword">確認密碼</label>

<input id="cconfirmpassword" name="confirmpassword" type="password" data-rule-equalTo="#cpassword" data-msg-equalTo="兩次密碼不一致">

</p>

<p>

<label for="cemail">郵箱</label>

<input id="cemail" name="email" data-rule-required="true" data-rule-email="true" data-msg-required="郵箱不可為空" data-msg-email="郵箱的格式不正確">

</input>

</p>

<p>

<label for="chasreferee">有推薦人請勾選</label>

<input type="checkbox" id="chasreferee" name="hasreferee">

</p>

<p>

<label for="creferee">推薦人</label>

<input id="creferee" name="referee" data-rule-required="#chasreferee:checked" data-msg-required="推薦人不可為空">

</input>

</p>

<p>

<input type="submit" value="提交">

</p>

</fieldset>

</form>

  看了之前的別人寫的文章,貌似是依賴jquery.metadata.js這個庫,然後寫的時候以 class=”required email” 這樣的形式來寫,這樣寫起來好像有些亂,class本身是呈現樣式的,現在被附上各種校正的規則,看上去似乎有些亂,不過好在新版本中,又有了新的寫法,不依賴上面的js庫,以 data-rule-驗證規則、data-msg-提示資訊 這樣的格式來重新定義,更簡單,更直觀,更強大了。上面的測試通過

  我的版本的jquery.validate1.13.js

  然後這樣的寫法,控制項中的messages不會生效,還會報錯:Cannot read property 'call' of undefined 園子裡面很多jquery.validate文章提到可以使用,我看是版本過時了,反正我沒有實驗出來。還有就是將驗證卸載class裡面我也是醉了。下面的測試錯誤!

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>無標題文檔</title>

<script type="text/javascript" src="jquery.min.js"></script>

 

<!--<script type="text/javascript" src="jquery.validate.js"></script>-->

<script type="text/javascript" src="jquery.validate1.13.js"></script>

<script type="text/javascript" src="jquery.validate.message_cn.js"></script>

<script type="text/javascript" src="jquery.metadata.js"></script>

<script type="text/javascript">

$(function(){

$.metadata.setType("attr", "validate");

$("#signupForm").validate();

//$("#signupForm").validate({ meta: "validate" });

//$("#commentForm").validate();

})

 

</script>

</head>

 

<body>

<form id="signupForm" method="get" action="">

<p>

 

 

<input id="email" name="email" validate="{required:true, email:true, messages:{required:'輸入email地址', email:'你輸入的不是有效郵件地址'}}" />

</p>

 

<p>

<input class="submit" type="submit" value="Submit"/>

</p>

</form>

 

</body>

</html>

  問題二:jQuery_validate配置後無論怎樣都看不到提示資訊。

  原因:submit()了二次。

  例子:

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>

<head>

<title>jquery test</title>

<script src="js/jquery.js"></script>

<script src="js/jquery.validate.js"></script>

<script src="js/jquery.metadata.js"></script>

<script src="js/messages_zh.js"></script>

 

<script>

$(document).ready(function() {

 

$("#commentForm").validate({

//debug:true

});

});

</script>

<script type="text/javascript">

function register(){

document.forms[0].action = 'register/addUser.action';

//document.forms[0].submit();

}

</script>

</head>

<body >

<form id="commentForm" method="post">

<table style ="width:100%">

<tr>

<td>user name:</td>

<td><input type="text" name="username" id="username" maxlength="10"

class="{required:true,minlength:6,maxlength:12}" /></td>

</tr>

<tr>

<td>password:</td>

<td><input type="password" name="password" id="password" maxlength="15"

class="required"/></td>

</tr>

<tr>

<td></td>

<td><input type="submit" value="Register" onclick="register();"></td>

</tr>

</table>

</form>

</body>

</html>

  jQuery驗證後有個提交,自己在register()中又提交了一次,將【document.forms[0].submit();】注釋掉後問題解決。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.