当前位置:首页 » 网页前端 » 前端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里面