當前位置:首頁 » 網頁前端 » 前端pattern屬性
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

前端pattern屬性

發布時間: 2022-06-27 18:13:00

① 為什麼在hmtl5加入了pattern屬性但是沒有用

jstllt;index;.prefix 目錄下的 (index拼接spring.view.hello);
return ;
lt;

import java;page1;)
public ModelAndView page1(){
//;
}
}

pom.xml添加依賴:
lt;time;
@RequestMapping(value = {;/
lt;jsp.beans;content;;/ 頁面位置 /content;;
return ;page//groupId; 從 application.properties 中讀取配置.util.Date,編碼如下:
package org.springboot;page2;)
public String page2(Model model){
/) 和 @RequestMapping 是有區別的
* 如果不寫參數,則為全局默認頁,加入輸入404頁面,也會自動訪問到這個頁面。
* 如果加了參數「.springframework;

@Controller
public class PageController {

//WEB-INF/page/page;**
* 響應到JSP頁面page1
*
* @return
* @author SHANHY
* @create 2016年1月5日
*/
@RequestMapping(;.tomcat.embedlt;;index;})
public String index(Maplt;groupId;org.apache.factory;
mav;page.jsp
ModelAndView mav = new ModelAndView(;String, Object,直接返回頁面字元串)
*
* @return
* @author SHANHY
* @create 2016年1月5日
*/
@RequestMapping(;/
return mav;
}

/**
* 響應到JSP頁面page1(可以直接使用Model封裝內容:Hello Shanhy};)
private String hello = ;Hello Shanhy;;,;.util.Map;

import org;
import org.springframework.ui.stereotype.Controller;
lt.springframework;
model;message;創建PageController;);artifactId;dependency;dependency; 直接返回字元串,框架默認會去 spring.view;
import java.Model; model){
//

/**
* 默認頁lt;br/scope;providedlt;.put(;
* @RequestMapping(;/dependency,則只認為是根頁面。
*
* @return
* @author SHANHY
* @create 2016年1月5日
*.jsp
model.addAttribute(;
lt;
import org.springframework.web.bind.sample.controller.annotation.RequestMapping;/ 本例為 /WEB-INF/jsp/index.jsp
model;
lt, hello);
lt;artifactId;, new Date());tomcat-embed-jasperlt;/).annotation.Value;
import org;
import org.springframework.web.servlet.ModelAndView;」;,如取不到默認值為Hello Shanhy
@Value(;${application.hell;groupId;javax.servletlt;page1;;
}

, this;scope;
lt;.suffix)頁面
/artifactId.put(;page/page1.addObject(, hello + ;(第二種);/groupId;/artifactId;
lt;/dependency;123456789

上面說了spring-boot 不JSP,想使用JSP需要配置application.properties。
添加src/main/resources/application.properties內容:
頁面默認前綴目錄
spring.mvc.view.prefix=/WEB-INF/jsp/
響應頁面默認後綴
spring.mvc.view.suffix=.jsp
自定義屬性,可以在Controller中讀取
application.hello=Hello Shanhy123456

在 src/main 下面創建 webapp/WEB-INF/jsp 目錄用來存放我們的jsp頁面。
index.jsp
lt;%@ page language=;java; pageEncoding=;UTF-8;%;
lt;!DOCTYPE HTML PUBLIC ;-//W3C//DTD HTML 4.01 Transitional//EN;;
lt;html;
lt;head;
lt;title;Spring Boot Samplelt;/title;
lt;/head;

lt;body;
Time: ${time}
lt;br;
Message: ${message}
lt;/body;
lt;/html;12345678910111213

page1.jsp
lt;%@ page language=;java; pageEncoding=;UTF-8;%;
lt;!DOCTYPE HTML PUBLIC ;-//W3C//DTD HTML 4.01 Transitional//EN;;
lt;html;
lt;head;
lt;title;Spring Boot Samplelt;/title;
lt;/head;

lt;body;
lt;h1;${content }lt;/h1;
lt;/body;
lt;/html;1234567891011

要想讓spring-boot支持JSP,需要將項目打成war包。
我們做最後一點修改,修改pom.xml文件,將 jar 中的 jar 修改為 war
然後啟動spring-boot服務。; 頁面位置 /WEB-INF/jsp/page

② 在html5 中怎麼簡單的編程,來校驗form中的內容

<inputtype="text"name=""pattern="[A-z]{3}"title="只能輸入3個英文字母"/>

這個表示這個輸入框只允許輸入英文字母且只能3個字母,後面的title是違反規則時的提示文字。

具體還有哪些寫法請參閱input的pattern屬性,隨便網上搜一下就有

③ HTML5 表單多條件驗證問題

1.輸入型控制項

Input type

用途

說明

email

電子郵件地址文本框

url

網頁URL文本框

number

數值的輸入域

屬性 值 描述
max number 規定允許的最大值
min number 規定允許的最小值
step number 規定合法的數字間隔(如果 step="3",則合法的數是 -3,0,3,6 等)
value number 規定默認值

range

特定值的范圍的數值,以滑動條顯示

屬性 值 描述
max number 規定允許的最大值
min number 規定允許的最小值
step number 規定合法的數字間隔(如果 step="3",則合法的數是 -3,0,3,6 等)
value number 規定默認值

Date pickers

日期,時間選擇器

僅Opera9+支持,包含date, month, week, time, datetime, datetime-local

search

用於搜索引擎,比如在站點頂部顯示的搜索框

與普通文本框用法一樣,只不過這樣更語文化

color

顏色選擇器

僅Opera支持

將原本type為text的input控制項聲明為以上特殊類型,是為了給用戶呈現不同的輸入界面(移動平台上支持這些不同的輸入界面,這里就不細說),而且表單提交時會對其值做進一步的驗證。下面展示這些新表單元素,請用支持這些表單元素的瀏覽器查看,IE對其支持最差。
E-mail:
date:
range: number: color:
2. 表單新特性和函數
2.1 placeholder
當用戶還沒有輸入值時,輸入型控制項可能通過placeholder向用戶顯示描述性說明文字或者提示信息,這在目前網站中很常見,一些JS框架都會提供類似功能,簡單的說下在舊版本中常用的解決方案,為輸入控制項創建一個label,然後通過CSS控制些label的位置使之覆蓋在輸入控制項上面,當label獲得焦點時,瀏覽器會把焦點指向輸入控制項。不過有了placeholder,新的瀏覽器就內置了這一功能,其特性值會以淺灰色樣式顯示在輸入框中,當輸入框獲得焦點並有值後,該提示信息自動消失。
如:

<p><label for="runnername">Runner:</label>
<input id="runnername"name="runnername" type="text" placeholder="First and last name" />
</p>

Runner:
2.2 autocomplete
其實在IE6中,autocomplete就已經實現,不過現在這一特性終於標准化了,瀏覽器通過autocomplete特性能夠知曉是否應該保存輸入值以備將來使用,autocomplete應該用一保護用戶敏感數據,避免本地瀏覽器對它們進行不安全的存儲

類型

作用

on

該欄位無需保護,值可以被保存和恢復

off

該欄位需要保護,值不可以保存

unspecified

包含<form>的默認設置,如果沒有被包含在表單中或沒有指定值,則行為表現為on

如:

<form action="" method="get" autocomplete="on">
Name:<input type="text" name="name" /><br />
E-mail: <input type="email" name="email" autocomplete="off" /><br />
<input type="submit" />
</form>

當用戶提交過一次表單後,再次訪問,name的輸入框會提示你曾輸入的值,而email則不會提示。
2.3 autofocus
頁面載入時,我們通過autofocus指定某個表單元素獲得焦點,但每個頁面只允許出現一個autofocus,如果設置多個則相當於未指定些行為。目前Opera10,Chromet和Safari瀏覽器支持。如果用戶有希望焦點轉移的情況下,使用使用autofocus會惹惱用戶。
2.5 list特性和datalist
通過使用list,開發人員能夠為某個輸入型控制項構造一個選值列表,其使用方法:

Webpage: <input type="url" list="url_list" name="link" />
<datalist id="url_list">
<option label="W3School" value="http://www.w3school.com.cn" />
<option label="Google" value="http://www.google.com" />
<option label="Microsoft" value="http://www.microsoft.com" />
</datalist>

Webpage:
請在Opera9+或Firefox10+瀏覽器中查看。
2.6 required
required 屬性規定必須在提交之前填寫輸入域(不能為空)。它是表單驗證最簡單的一種方式方法,使用方法:

Name: <input type="text" name="usr_name" required="required" />

2.7 pattern
pattern 屬性規定用於驗證 input 域的模式(pattern),模式(pattern) 是正則表達式。那些type為email或url的輸入控制項內置相關正則表達式,如果value不符合其正則表達式,那表單將通不過驗證,無法提交。使用方法:

Country code: <input type="text" name="country_code"
pattern="[A-z]{3}" title="Three letter country code" />

2.8 novalidate
novalidate 屬性規定在提交表單時不應該驗證 form 或 input 域。
如:

<form action="demo_form.asp" method="get" novalidate="true">
E-mail: <input type="email" name="user_email" />
<input type="submit" />
</form>

3. 表單驗證
表單驗證是一套系統,它為終端用戶檢測無效的數據並標記這些錯誤,是一種用戶體驗的優化,讓web應用更快的拋出錯誤,但它仍不能取代伺服器端的驗證,重要數據還要要依賴於伺服器端的驗證,因為前端驗證是可以繞過的。
目前任何錶單元素都有八種可能的驗證約束條件:

名稱

用途

用法

valueMissing

確保控制項中的值已填寫

將required屬性設為true,
<input type="text"required="required"/>

typeMismatch

確保控制項值與預期類型相匹配

<input type="email"/>

patternMismatch

根據pattern的正則表達式判斷輸入是否為合法格式

<input type="text" pattern="[0-9]{12}"/>

toolong

避免輸入過多字元

設置maxLength,<textarea id="notes" name="notes" maxLength="100"></textarea>

rangeUnderflow

限制數值控制項的最小值

設置min,<input type="number" min="0" value="20"/>

rangeOverflow

限制數值控制項的最大值

設置max,<input type="number" max="100" value="20"/>

stepMismatch

確保輸入值符合min,max,step的設置

設置max min step,<input type="number" min="0" max="100" step="10" value="20"/>

customError

處理應用代碼明確設置能計算產生錯誤

例如驗證兩次輸入的密碼是否一致,等會DEMO細說

下面展現瀏覽器自帶的驗證功能請在Chrome、Opera或Firefox中查看:
源代碼:

<form name="register1" id="register1">
<p><label for="runnername">RunnerName:</label>
<input id="runnername"name="runnername" type="text" placeholder="First and last name" required="required" autofocus="autofocus"/>
</p>
<p><label for="phone">Tel #:</label>
<input id="phone" name="phone" type="text" pattern="\d{3}-\d{4}-\d{4}"
placeholder="xxx-xxxx-xxxx"/></p>
<p><label for="emailaddress">E-mail:</label>
<input id="emailaddress" name="emailaddress" type="email"
placeholder="For confirmation only"/></p>
<p><label for="dob">DOB:</label>
<input id="dob" name="dob" type="date"
placeholder="MM/DD/YYYY"/></p>
<p>Count:<input type="number" id="count" name="count" min="0" max="100" step="10"/></p>
<p><label for="style">Shirt style:</label>
<input id="style" name="style" type="text" list="stylelist" title="Years of participation"
autocomplete="off"/></p>
<datalist id="stylelist">
<option value="White" label="1st Year"/>
<option value="Gray" label="2nd - 4th Year"/>
<option value="Navy" label="Veteran (5+ Years)"/>
</datalist>

<fieldset>
<legend>Expectations:</legend>
<p>
<label for="confidence">Confidence:</label>
<input id="confidence" name="level" type="range"
onchange="setConfidence(this.value)"
min="0" max="100" step="5" value="0"/>
<span id="confidenceDisplay">0%</span></p>
<p><label for="notes">Notes:</label>
<textarea id="notes" name="notes" maxLength="100"></textarea></p>
</fieldset>

<p><input type="submit" name="register" value="Submit" onclick=" checkForm()"/></p>
</form>

④ HTML5真正的新特性是什麼

1. 新的Doctype
盡管使用<!DOCTYPE html>,即使瀏覽器不懂這句話也會按照標准模式去渲染
2. Figure元素
用<figure>和<figcaption>來語義化地表示帶標題的圖片
<figure>
<img src=」path/to/image」 alt=」About image」 />
<figcaption>
<p>This is an image of something interesting. </p>
</figcaption>
</figure>
3. 重新定義的<small>
<small>已經被重新定義了,現在被用來表示小的排版,如網站底部的版權聲明
4. 去掉link和script標簽裡面的type屬性
5. 加/不加 括弧
HTML5沒有嚴格的要求屬性必須加引號,閉合不閉合,但是建議加上引號和閉合標簽
6. 讓你的內容可編輯,只需要加一個contenteditable屬性
7. Email Inputs
如果我們給Input的type設置為email,瀏覽器就會驗證這個輸入是否是email類型,當然不能只依賴前端的校驗,後端也得有相應的校驗
8. Placeholders
這個input屬性的意義就是不必通過javascript來做placeholder的效果了
9. Local Storage
使用Local Storage可以永久存儲大的數據片段在客戶端(除非主動刪除),目前大部分瀏覽器已經支持,在使用之前可以檢測一下window.localStorage是否存在
10. 語義化的header和footer
11. 更多的HTML5表單特性
12. IE和HTML5
默認的,HTML5新元素被以inline的方式渲染,不過可以通過下面這種方式讓
其以block方式渲染
header, footer, article, section, nav, menu, hgroup {
display: block;
}
不幸的是IE會忽略這些樣式,可以像下面這樣fix:
document.createElement(」article」);
document.createElement(」footer」);
document.createElement(」header」);
document.createElement(」hgroup」);
document.createElement(」nav」);
document.createElement(」menu」);
13. hgroup
一般在header裡面用來將一組標題組合在一起,如
<header>
<hgroup>
<h1> Recall Fan Page </h1>
<h2> Only for people who want the memory of a lifetime. </h2>
</hgroup>
</header>
14. Required屬性
required屬性定義了一個input是否是必須的,你可以像下面這樣聲明
<input type=」text」 name=」someInput」 required>
或者
<input type=」text」 name=」someInput」 required=」required」>
15. Autofocus屬性
正如它的詞義,就是聚焦到輸入框裡面
<input type=」text」 name=」someInput」 placeholder=」Douglas Quaid」 required autofocus>
16. Audio支持
HTML5提供了<audio>標簽,你不需要再按照第三方插件來渲染音頻,大多數現代瀏覽器提供了對於HTML5 Audio的支持,不過目前仍舊需要提供一些兼容處理,如
<audio autoplay=」autoplay」 controls=」controls」>
<source src=」file.ogg」 /><!–FF–>
<source src=」file.mp3″ /><!–Webkit–>
<a href=」file.mp3″>Download this file.</a>
</audio>
17. Video支持
和Audio很像,<video>標簽提供了對於video的支持,由於HTML5文檔並沒有給video指定一個特定的編碼,所以瀏 覽器去決定要支持哪些編碼,導致了很多不一致。Safari和IE支持H.264編碼的格式,Firefox和Opera支持Theora和Vorbis 編碼的格式,當使用HTML5 video的時候,你必須都提供:
<video controls preload>
<source src=」cohagenPhoneCall.ogv」 type=」video/ogg; codecs=』vorbis, theora』」 />
<source src=」cohagenPhoneCall.mp4″ type=」video/mp4; 』codecs=』avc1.42E01E, mp4a.40.2′」 />
<p> Your browser is old. <a href=」cohagenPhoneCall.mp4″>Download this video instead.</a> </p>
</video>
18. 預載入視頻
preload屬性就像它的字面意思那麼簡單,你需要決定是否需要在頁面載入的時候去預載入視頻
<video preload>
19. 顯示視頻控制
<video preload controls>
20. 正則表達式
由於pattern屬性,我們可以在你的markup裡面直接使用正則表達式了
<form action=」" method=」post」>
<label for=」username」>Create a Username: </label>
<input type=」text」 name=」username」 id=」username」 placeholder=」4 <> 10″ pattern=」[A-Za-z]{4,10}」 autofocus required>
<button type=」submit」>Go </button>
</form>
21. 檢測屬性支持
除了Modernizr之外我們還可以通過javascript簡單地檢測一些屬性是否支持,如:
<script>
if (!』pattern』 in document.createElement(』input』) ) {
// do client/server side validation
}
</script>
22. Mark元素
把<mark>元素看做是高亮的作用,當我選擇一段文字的時候,javascript對於HTML的markup效果應該是這樣的:
<h3> Search Results </h3>
<p> They were interrupted, just after Quato said, <mark>」Open your Mind」</mark>. </p>
23. 什麼時候用<div>
HTML5已經引入了這么多元素,那麼div我們還要用嗎?div你可以在沒有更好的元素的時候去用。
24. 想立即使用HTML5?
不要等2022了,現在就可以使用了,just do it.
25. 哪些不是HTML5
1)SVG
2)CSS3
3)Geolocation
4)Client Storage
5)Web Sockets
26. Data屬性
<div id=」myDiv」 data-custom-attr=」My Value」> Bla Bla </div>
CSS中使用:
<style>
h1:hover:after {
content: attr(data-hover-response);
color: black;
position: absolute;
left: 0;
}
</style>
<h1 data-hover-response=」I Said Don』t Touch Me!」> Don』t Touch Me </h1>
27. Output元素
<output>元素用來顯示計算結果,也有一個和label一樣的for屬性
28. 用Range Input來創建滑塊
HTML5引用的range類型可以創建滑塊,它接受min, max, step和value屬性
可以使用css的:before和:after來顯示min和max的值
<input type=」range」 name=」range」 min=」0″ max=」10″ step=」1″ value=」">
input[type=range]:before { content: attr(min); padding-right: 5px;
}
input[type=range]:after { content: attr(max); padding-left: 5px;}

⑤ 如何用js控制文本框只能輸入數字和逗號組成的字元串

提供幾個思路:
1. 用HTML5中input的pattern屬性,他是指定一個正則表達式,輸入的內容必須符合。像這種格式,用「(\d+,)+\d+」這樣的正則就行。缺點是Safari和IE10以下不支持。

2. input的onchange事件,每次都獲取字元串,取裡面的每個字元,判斷是否為數字或逗號。

3. input的onkeyup/ onkeydown事件,每次按鍵就觸發,判斷按鍵代碼是否為數字鍵或逗號鍵。缺點是按數字鍵不一定輸入數字,還需要一塊判斷是否按下shift等。

還是2好一點。

⑥ HTMLform表單既想保留pattern驗證功能,又想在驗證正確之後執行函數,該怎麼操作

考慮在 onsubmit 事件里捕獲驗證成功,然後做餘下的事情

⑦ jstlfmt標簽pattern屬性設置為yyyy-mm-dd hh:mm:ss可以比較嗎

<input name="_txtIncludeDate" type="text" style="border:0" readonly="true" value="<fmt:formatDate pattern="yyyy/MM/dd HH/mm/ss" value="${PM007RowData[2]}">
"/>
直接放在value裡面