Click to See Complete Forum and Search --> : easy script question


esthera
12-02-2003, 03:09 AM
can someone provide for me the script that I would put to only let someone enter an integer in a certian textfield on a form?

Gollum
12-02-2003, 04:18 AM
Something like this would do...

<html>
<head>
<script type="text/javascript">

function validCode(kc)
{
switch ( kc )
{
case 8: // backspace
case 46: // delete
case 37: // left arrow
case 39: // right arrow
case 36: // home
case 35: // end
return true;
}

var c = String.fromCharCode(kc);
return ( (c >= '0') && (c <= '9') )
}

function onKeyDown(oThis, evt)
{
if ( document.all )
{
evt = window.event;
if ( !validCode(evt.keyCode) ) evt.returnValue = false;
return true;
}
else
{
return validCode(evt.which);
}
}

function validate()
{
window.oFocus.value = window.oFocus.value.replace(/[^0-9]/g,'');
window.oFocus.ctxt = window.setTimeout('validate();',100);
}

</script>
</head>
<body>
<input
type=text
onkeydown="return onKeyDown(this,event);"
onfocus="window.oFocus = this; this.ctxt = window.setTimeout('validate();',100);"
onblur="window.clearTimeout(this.ctxt);">
</body>
</html>

I used a timer scheme here as there are ways to enter text into the text box that the text box doesn't notice (right-click paste is one)

Pittimann
12-02-2003, 04:49 AM
Hi!

I had a different solution to that question but due to the fact that it was not a validation "on the fly" I have to admit, that Gollum's proposal is much closer to esthera's question.

Anyway - one modification should be done. Your solution - Gollum - doesn't allow the user to enter negative integers. For that I altered your validate function:

function validate()
{
if (window.oFocus.value.substring(0,1)=="-"){
window.oFocus.value = "-"+window.oFocus.value.replace(/[^0-9]/g,'');
}
else{
window.oFocus.value = window.oFocus.value.replace(/[^0-9]/g,'');
}
window.oFocus.ctxt = window.setTimeout('validate();',100);
}

Would you agree with that?

Cheers - Pit

esthera
12-02-2003, 11:57 AM
gollum,
your solution works very good except it doesn't let users enter numbers in the number keypad.

Is their a solution for that.

Thanks for your help,
Esther

Pittimann
12-02-2003, 12:01 PM
Hi!

Could you live with a solution which doesn't work "on the fly" but checks the input after it has been entered instead?

Cheers - Pit

esthera
12-02-2003, 10:49 PM
Pittiaman: Do you mean when the user leaves the text box or when the form is submitted? If it is when the user leaves the textbox that would be fine.

Pittimann
12-03-2003, 01:56 AM
Hi!

One thing in advance: I don't see a reason why gollum's script shouldn't work with entries made from the numeric keypad.

esthera, in the code below, I followed your wish, that the textfield's content is checked when the user leaves it. Here it is:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Integers</title>
</head>
<script type="text/JavaScript">
function checkInteger(){
enteredValue=document.myForm.onlyIntegers.value;
var checkDot=0;
for (var i=0; i<enteredValue.length;i++){
if (enteredValue.substring(i,i+1)==".")
checkDot++;
}
if (isNaN(document.myForm.onlyIntegers.value)||checkDot>0){
alert("Only integers allowed!");
document.myForm.onlyIntegers.focus();
document.myForm.onlyIntegers.select();
return false;
}
else {
return true;
}
}
</script>
<body>
<form name="myForm" method="post" action = "yourActionHere">
<input type="text" name="onlyIntegers" onblur="checkInteger()"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

Cheers - Pit

esthera
12-07-2003, 11:55 AM
can anyone figure out how to get gollum's script working with the keypad. I like how it works except that I need the keypad to work as well.

Pittimann
12-07-2003, 12:05 PM
Hi!

I have to ask a very odd question, because Gollum's script worked fine with both parts of the keypad (alphanumeric and numeric - PC, Win98, IE 6.0):

Is your Num Lock off??

Cheers - Pit

esthera
12-07-2003, 12:42 PM
no num lock is on and I am using ie 6

Pittimann
12-07-2003, 12:59 PM
Really sorry - no any idea... :confused:

maybe somebody else???

Good luck - Pit

khayman2001
12-14-2003, 02:24 PM
Here's another option for you:

var correct_chars = "-1234567890";

function CheckIt(the_value)
{
var the_last_char = the_value.charAt(the_value.length-1);
var index_of_last_char = correct_chars.indexOf(the_last_char);
if(index_of_last_char == -1)
{
alert("Sorry, integers only.");
var new_value = the_value.substring(0, the_value.length-1);
window.document.the_form.the_field.value = new_value;
}
else
{
return true;
}
}


and in the body:

<form name='the_form'>
<input type='text' name='the_field' onKeyUp='CheckIt(window.document.the_form.the_field.value);'>


Granted, there's no way to keep them from pasting, but its good for a rudimentary fix, and a lot shorter to boot.