Onkeyup="this.value = this.value.replace (/\\D/g, '')" and single DOT

I am using this attribute of <input> tag:

onkeyup=“this.value = this.value.replace (/\D/g, ‘’)”

to restrict input field values only to integers. How can I allow a single dot
in input?

For instanse: 1234.1234
but not 1234.1234.1234

Thanks

Have you tried:

this.value.replace (/[^\\d\\.]/g, '')

In other words, everything that is not (digit or dot).

How can I allow a single dot
in input?
Regular expressions don’t restrict input–they look for matches in a string.

but not 1234.1234.1234

What do you want to do if the user enters that?

This -> this.value.replace (/[^\d\.]/g, ‘’) works fine but allows multiple dot entries.

If there are more than one dot I want to give an alert message to the user that he/she can’t use multiple dots. However, better is to restrict multiple dot entries.

Thanks.

If there are more than one dot I want to give an alert message to the user that he/she can’t use multiple dots.

var piecesArray = elmt.value.split(".");
if(piecesArray.length > 2) alert("How about some valid input?!");

However, better is to restrict multiple dot entries.

You can check the key codes of each keypress, and keep track of the number of decimal points entered. If the number is greater than 1, you can cancel the keypress, alert a message, etc. Here’s how:

  1. Moz automatically sends the event object, which contains the key code of the key that was pressed, to the event handler function. So, you need to ‘catch’ the event object by adding a parameter to your function:
function cancel_Key([color="red"]e[/color])
{
	....
	....
	
}

IE does not automatically send the event object to the function, so you have to summon it manually:

function cancel_Key(e)
{
	[color="red"]if (!e) var e = window.event;[/color]  //for IE
	
}

After that, you will have the event object stored in the variable ‘e’.

  1. There are also cross browser differences as to where the key code is stored in the event object ‘e’. In IE, it is in the .keyCode property. In Moz, it can be the .which property. So, you need to do something like this:
function cancel_Key(e)
{
	if (!e) var e = window.event;  //for IE
	
	[color="red"]var code;
	if(e.keyCode) code = e.keyCode; //for IE
	if(e.which) code = e.which;  //for other browsers[/color]
}

After that, the variable ‘code’ will have the key code.

  1. Then, all you have to do is test for the keycode you are interested in canceling, and return false to cancel the keypress:
function cancel_Key(e)
{
	if (!e) var e = window.event;  //for IE
	
	var code;
	if(e.keyCode) code = e.keyCode; //for IE
	if(e.which) code = e.which;  //for other browsers
	alert(code);  //this will identify the code of the key you press
	[color="red"]if (code == <the code you are interested in responding to>)) return false;
	else return true;[/color]
}

Some browsers allow the canceling of the onkeydown, onkeyup, and onkeypress events, and some don’t, so that can be another problem. I believe onkeydown is not cancelable in FF1.0, where onkeypress is cancelable in IE6 and FF1.0.

  1. Now, how do you use that function? If you want to cancel the key on the whole page:
window.onload=function()
{
	document.onkeypress = cancel_Key;
};
</script>
</head>

If you want to cancel the key on only a form:


window.onload=function()
{
	document.my_form.onkeypress = cancel_Key;
};
</script>
</head>

Thanks for such a great reply!