input型別和限制

iDotNetSpace發表於2008-09-23
常用限制input的方法
1.取消按鈕按下時的虛線框,在input裡新增屬性值 hideFocus 或者 HideFocus=true
<input type="submit" value="提交" hidefocus="true" />

2.只讀文字框內容,在input裡新增屬性值 readonly
<input type="text" readonly />

3.防止退後清空的TEXT文件(可把style內容做做為類引用)
<input type="text" style="behavior.:url(#default#savehistory);" />

4.ENTER鍵可以讓游標移到下一個輸入框
<input type="text" nkeydown="if(event.keyCode==13)event.keyCode=9" />

5.只能為中文(有閃動)
<input type="text" nkeyup="value=value.replace(/[ -~]/g,'')" nkeydown="if(event.keyCode==13)event.keyCode=9" />

6.只能為數字(有閃動)
<input type="text" nkeyup="value=value.replace(/[^"d]/g,'') " nbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^"d]/g,''))" />

7.只能為數字(無閃動)
<input type="text" style="ime-mode:disabled" nkeydown="if(event.keyCode==13)event.keyCode=9" nkeypress="if ((event.keyCode<48 || event.keyCode>57)) event.returnValue=false" />

8.只能輸入英文和數字(有閃動)
<input type="text" nkeyup="value=value.replace(/["W]/g,'')" nbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^"d]/g,''))" />

9.遮蔽輸入法
<input type="text" name="url" style="ime-mode:disabled" nkeydown="if(event.keyCode==13)event.keyCode=9" />

10. 只能輸入 數字,小數點,減號(-) 字元(無閃動)
<input nkeypress="if (event.keyCode!=46 && event.keyCode!=45 && (event.keyCode<48 || event.keyCode>57)) event.returnValue=false" />

11. 只能輸入兩位小數,三位小數(有閃動)
<input type="text" maxlength="9" nkeyup="if(value.match(/^"d{3}$/))value=value.replace(value,parseInt(value/10)) ;value=value.replace(/"."d*"./g,'.')" nkeypress="if((event.keyCode<48 || event.keyCode>57) && event.keyCode!=46 && event.keyCode!=45 || value.match(/^"d{3}$/) || /"."d{3}$/.test(value)) {event.returnValue=false}" />
回答者:cld007 - 助理 三級 11-24 14:48
提問者對於答案的評價:
謝謝.. 好詳細額!
評價已經被關閉 目前有 0 個人評價

50% (0)
不好
50% (0)
其他回答
共 2 條
http://www.w3.org/TR/REC-html32.html

HTML標準的RFC

type
Used to set the type of input field:

type=text (the default)
A single line text field whose visible size can be set using the size attribute, e.g. size=40 for a 40 character wide field. Users should be able to type more than this limit though with the text scrolling through the field to keep the input cursor in view. You can enforce an upper limit on the number of characters that can be entered with the maxlength attribute. The name attribute is used to name the field, while the value attribute can be used to initialize the text string shown in the field when the document is first loaded.

<input type=text size=40 name=user value="your name">

type=password
This is like type=text, but echoes characters using a character like * to hide the text from prying eyes when entering passwords. You can use size and maxlength attributes to control the visible and maximum length exactly as per regular text fields.

<input type=password size=12 name=pw>

type=checkbox
Used for simple Boolean attributes, or for attributes that can take multiple values at the same time. The latter is represented by several checkbox fields with the same name and a different value attribute. Each checked checkbox generates a separate name/value pair in the submitted data, even if this results in duplicate names. Use the checked attribute to initialize the checkbox to its checked state.

<input type=checkbox checked name=uscitizen value=yes>

type=radio
Used for attributes which can take a single value from a set of alternatives. Each radio button field in the group should be given the same name. Radio buttons require an explicit value attribute. Only the checked radio button in the group generates a name/value pair in the submitted data. One radio button in each group should be initially checked using the checked attribute.

<input type=radio name=age value="0-12">
<input type=radio name=age value="13-17">
<input type=radio name=age value="18-25">
<input type=radio name=age value="26-35" checked>
<input type=radio name=age value="36-">

type=submit
This defines a button that users can click to submit the form's contents to the server. The button's label is set from the value attribute. If the name attribute is given then the submit button's name/value pair will be included in the submitted data. You can include several submit buttons in the form. See type=image for graphical submit buttons.

<input type=submit value="Party on ...">

type=image
This is used for graphical submit buttons rendered by an image rather than a text string. The URL for the image is specified with the src attribute. The image alignment can be specified with the align attribute. In this respect, graphical submit buttons are treated identically to IMG elements, so you can set align to left, right, top, middle or bottom. The x and y values of the location clicked are passed to the server: In the submitted data, image fields are included as two name/value pairs. The names are derived by taking the name of the field and appending ".x" for the x value, and ".y" for the y value.

Now choose a point on the map:

<input type=image name=point src="map.gif">

Note: image fields typically cause problems for text-only and speech-based user agents!
type=reset
This defines a button that users can click to reset form. fields to their initial state when the document was first loaded. You can set the label by providing a value attribute. Reset buttons are never sent as part of the form's contents.

<input type=reset value="Start over ...">

type=file
This provides a means for users to attach a file to the form's contents. It is generally rendered by text field and an associated button which when clicked invokes a file browser to select a file name. The file name can also be entered directly in the text field. Just like type=text you can use the size attribute to set the visible width of this field in average character widths. You can set an upper limit to the length of file names using the maxlength attribute. Some user agents support the ability to restrict the kinds of files to those matching a comma separated list of MIME content types given with the ACCEPT attribute e.g. accept="image/*" restricts files to images. Further information can be found in RFC 1867.

<input type=file name=photo size=20 accept="image/*">

type=hidden
These fields should not be rendered and provide a means for servers to store state information with a form. This will be passed back to the server when the form. is submitted, using the name/value pair defined by the corresponding attributes. This is a work around for the statelessness of HTTP. Another approach is to use HTTP "Cookies".

<input type=hidden name=customerid value="c2415-345-8563">

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-462653/,如需轉載,請註明出處,否則將追究法律責任。

相關文章