Jump to content

form input types


deved

Recommended Posts

Anyone know how to force a form input field to accept numbers only for postcodes or phone numbers? There are options to make the field 'hidden', 'readonly', 'password', and even limit the number of characters using maxlength. Have searched the forums to no avail.Cheers :)

Link to comment
Share on other sites

You need to use javascript for some of it.Here's what you can do without java script:Password field: <input type="password" />Read only: <input type="text" readonly="readonly" />Hidden input (you can use it to transfer data written by a server side language: <input type="hidden" name="name" value="something" />Limit the number of characters:Put this in the <head> of your document:

<script type="text/javascript">function charLimit(obj) {if(obj.length > 15) return false;}</script>

This is the input field:

<input type="text" onkeydown="charLimit(this.value)"; />

Only allow numbers:This goes in the <head>:

<script type="text/javascript">function onlyNumbers(e) {if(e.keyCode < 48 || e.keyCode > 57) return false}</script>

This goes where you want the text field to appear:

<input type="text" id="textField" /><script type="text/javascript">document.getElementById("textField").onkeydown = onlyNumbers;</script>

Link to comment
Share on other sites

You need to use javascript for some of it.Here's what you can do without java script:Password field: <input type="password" />Read only: <input type="text" readonly="readonly" />Hidden input (you can use it to transfer data written by a server side language: <input type="hidden" name="name" value="something" />Limit the number of characters:Put this in the <head> of your document:
<script type="text/javascript">function charLimit(obj) {if(obj.length > 15) return false;}</script>

This is the input field:

<input type="text" onkeydown="charLimit(this.value)"; />

Only allow numbers:This goes in the <head>:

<script type="text/javascript">function onlyNumbers(e) {if(e.keyCode < 48 || e.keyCode > 57) return false}</script>

This goes where you want the text field to appear:

<input type="text" id="textField" /><script type="text/javascript">document.getElementById("textField").onkeydown = onlyNumbers;</script>

Thanks Ingolme, that did the trick. Can I ask how the formula in 'function onlyNumbers(e)' works?
Link to comment
Share on other sites

onlyNumber receives an event (e).That event contains data, the data of the key that was pressed is e.keyCode.If e.keyCode is not within the ASCII character codes between "0" and "9" (ASCII characters from 48 to 57) then do not write the character.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...