Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
可以在input上直接綁定正則,可以自定義datatype,自定義datatype可以是正則,也可以是函數(shù),datatype可以累加或單選,甚至還可以對(duì)datatype規(guī)則執(zhí)行簡(jiǎn)單的邏輯運(yùn)算。內(nèi)置10類(lèi)常見(jiàn)的格式驗(yàn)證;
可以自定義提示方式,可以實(shí)現(xiàn)你想要的任何提示效果。內(nèi)置了4種常見(jiàn)的提示方式;
可以對(duì)表單下的某一塊區(qū)域或具體的某個(gè)表單元素單獨(dú)進(jìn)行驗(yàn)證,并可以選擇驗(yàn)證后需不需要顯示出錯(cuò)信息,還能得到一個(gè)值來(lái)判斷被檢測(cè)對(duì)象是否通過(guò)了驗(yàn)證;
可以輕松的取消或恢復(fù)對(duì)表單下的某一塊區(qū)域或具體的某個(gè)表單元素的驗(yàn)證;
強(qiáng)大的ajax功能,很輕松的可以實(shí)現(xiàn)實(shí)時(shí)驗(yàn)證以及表單的ajax提交;可以靈活的設(shè)置ajax提交時(shí)的參數(shù);
智能的出錯(cuò)信息提示:會(huì)根據(jù)綁定的datatype輸出相應(yīng)的出錯(cuò)信息,另外還可以在自定義datatype里返回具體的出錯(cuò)信息,錯(cuò)誤信息里可以使用html標(biāo)簽,如果頁(yè)面里沒(méi)有顯示出錯(cuò)信息的標(biāo)簽,會(huì)根據(jù)tiptype值自動(dòng)創(chuàng)建。可以選擇在沒(méi)有輸入時(shí)不提示和只在提交表單時(shí)有信息提示。可以選擇一次提示單個(gè)錯(cuò)誤或一次顯示全部出錯(cuò)信息。可以自己設(shè)置默認(rèn)的提示文字;
可以在表單開(kāi)始檢測(cè)前和表單檢測(cè)通過(guò)后,提交表單之前綁定事件;
當(dāng)前版本外調(diào)插件可以實(shí)現(xiàn)文件上傳檢測(cè)、密碼強(qiáng)度檢測(cè)、日期控件和表單美化效果;
豐富的Validform對(duì)象的屬性和方法,給你的驗(yàn)證操作帶來(lái)無(wú)限的可能。
1、引入css
請(qǐng)查看下載文件中的style.css,把里面Validform必須部分復(fù)制到你的css中(文件里這個(gè)注釋 "/*==========以下部分是Validform必須的===========*/" 之后的部分是必須的)。之前發(fā)現(xiàn)有部分網(wǎng)友把整個(gè)style.css都引用在了頁(yè)面里,然后發(fā)現(xiàn)樣式?jīng)_突了。
2、引入js (jquery 1.4.3 以上版本都可以)
<script type="text/javascript" src="http://validform.rjboy.cn/wp-content/themes/validform/js/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="http://validform.rjboy.cn/Validform/v5.1/Validform_v5.1_min.js"></script>
3、給需要驗(yàn)證的表單元素綁定附加屬性
<form class="demoform">
<input type="text" value="" name="name" datatype="s5-16" errormsg="昵稱(chēng)至少5個(gè)字符,最多16個(gè)字符!" />
</form>
4、初始化,就這么簡(jiǎn)單
<script type="text/javascript">
$(function(){
$(".demoform").Validform();
})
</script>
官方下載地址:http://validform.rjboy.cn/download.html
篇文篇主要簡(jiǎn)述如何在springboot中驗(yàn)證表單信息。在springmvc工程中,需要檢查表單信息,表單信息驗(yàn)證主要通過(guò)注解的形式。
構(gòu)建工程
創(chuàng)建一個(gè)springboot工程,由于用到了 web 、thymeleaf、validator、el,引入相應(yīng)的起步依賴(lài)和依賴(lài),代碼清單如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-el</artifactId>
</dependency>
</dependencies>
創(chuàng)建一個(gè)PresonForm的Object類(lèi)
package com.forezp.entity; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; /** * Created by fangzhipeng on 2017/4/19. */ public class PersonForm { @NotNull @Size(min=2, max=30) private String name; @NotNull @Min(18) private Integer age; public String getName() { return this.name; } public void setName(String name) { this.name=name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age=age; } public String toString() { return "Person(Name: " + this.name + ", Age: " + this.age + ")"; } }
這個(gè)實(shí)體類(lèi),在2個(gè)屬性:name,age.它們各自有驗(yàn)證的注解:
創(chuàng)建 web Controller
@Controller public class WebController extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/results").setViewName("results"); } @GetMapping("/") public String showForm(PersonForm personForm) { return "form"; } @PostMapping("/") public String checkPersonInfo(@Valid PersonForm personForm, BindingResult bindingResult) { if (bindingResult.hasErrors()) { return "form"; } return "redirect:/results"; } }
創(chuàng)建form表單
src/main/resources/templates/form.html:
<html>
<body>
<form action="#" th:action="@{/}" th:object="${personForm}" method="post">
<table>
<tr>
<td>Name:</td>
<td><input type="text" th:field="*{name}" /></td>
<td th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name Error</td>
</tr>
<tr>
<td>Age:</td>
<td><input type="text" th:field="*{age}" /></td>
<td th:if="${#fields.hasErrors('age')}" th:errors="*{age}">Age Error</td>
</tr>
<tr>
<td><button type="submit">Submit</button></td>
</tr>
</table>
</form>
</body>
</html>
注冊(cè)成功的頁(yè)面
src/main/resources/templates/results.html:
<html>
<body>
Congratulations! You are old enough to sign up for this site.
</body>
</html>
演示
啟動(dòng)工程,訪問(wèn)http://localhost:8080/:
如果你輸入A和15,點(diǎn)擊 submit:
如果name 輸入N, age為空:
如果輸入:forezp. 18
參考資料
https://spring.io/guides/gs/validating-form-input/
## 利用forms表單組件進(jìn)行表單校驗(yàn),完成用戶(hù)名,密碼,確認(rèn)密碼,郵箱功能的校驗(yàn)
該作業(yè)包含了下面的知識(shí)點(diǎn):
error_messages,label,required,invalid,局部鉤子函數(shù),全局鉤子函數(shù),
forms_obj.cleaned_data,forms_obj.errors,locals(),
{{ forms.label }}:{{ forms }},{{ forms.errors.0 }}
### urls.py文件
```
*請(qǐng)認(rèn)真填寫(xiě)需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。