Click to See Complete Forum and Search --> : Simple Validation Check....


Superfly1611
09-15-2003, 04:49 AM
Hi,

I'm trying to write a simple client side validation script that doesn't allow users to enter decimal numbers into a quantity field.

Is there a function in Javascript that allows me to do that?


Cheers

Superfly

Charles
09-15-2003, 06:03 AM
<input type="text" onchange="if (/\D/.test(this.value)) {alert('Please enter an integer value.'); this.value = ''; this.focus()}">

Webskater
09-15-2003, 06:53 AM
function checknum
{
//make sure keys are numeric
var key = event.keyCode;
if (key<48 || key>57) event.returnValue=false;
}

<input type=text onkeydown="checknum();">

This only allows users to type the numerals 0,1,2,3,4,5,6,7,8,9 in the text box.

Superfly1611
09-15-2003, 09:38 AM
Cheers for that guys...

Much Appreciated

Superfly1611
09-19-2003, 08:02 AM
Hi guys - me again - my script nearly works now.

Infact the only thing thats wrong at the mo is the fact that if i hold down shift + 0-9 it enters in !"£$.

I thought it would stop that as teh characters(!"£$%^&*etc) are are below 48 in the keycode list that i found... but apparently not.

As it stands the function looks like this...


function checknum()
{
var key = event.keyCode;
if(key < 48)
{
event.returnValue=false;
}
if(key > 57)
{
event.returnValue=false;
}
if(key == 8)
{
event.returnValue=true;
}

}

any ideas?

pyro
09-19-2003, 08:29 AM
Use Charles solution, which uses a regular expression to check the field and make sure it only contains digits.

Superfly1611
09-19-2003, 08:37 AM
yeah - i was going to but if possible i'd like to stop tehm from entering in the wrong digits in teh first place.

pyro
09-19-2003, 08:40 AM
switch onchange for onkeyup, and it will validate as you go...

Superfly1611
09-19-2003, 09:39 AM
didn't think of that - cheers - works fine

pyro
09-19-2003, 09:40 AM
No problem...