PDA

View Full Version : Allow only 300,400,500,600,700,800 etc


ksridhar69
11-20-2002, 07:28 PM
onblur I want to allow only these numbers 300,400,500,600,700,800,900,1000,1100,1200,1400,1500,1600,1700,1800,1900,2000

Is it possible? If user enters other than above numbers alert the user




function allowonlythesenumbers()
{

alert("Only these numbers 300,400,500,600,700,800,900,1000,1100,1200,1400,1500,1600,1700,1800,1900,2000 are allowed")
}

</script>
<input type=text name=a1 onBlur="allowonlythesenumbers()">

Borgtex
11-20-2002, 07:58 PM
Somebody else will post the usual and logic answer. Let me be weird & creative :cool:

function checknum(num)
{
allwnums='#300#400#500#600#700#800#900#1000#1100#1200#1400#1500#1600#1700#1800#1900#2000#'

num='#'+num+'#'
if (allwnums.indexOf(num)==-1) alert ('Wrong number; only ' +allwnums.substring(1,allwnums.length-1).split('#').join(',')+' allowed')
}

beetle
11-20-2002, 07:59 PM
Well, this could be done with a regular expression, but this method might be a bit easier...
function allowonlythesenumbers(elem) {
var val = parseInt(elem.value, 10);
if (val >= 300 && val <= 2000 && val % 100 == 0)
return true;
else {
alert("Only these numbers 300,400,500,600,700,800,900,1000,1100,1200,1400,1500,1600,1700,1800,1900,2000 are allowed");
return false;
}
}

<input type=text name=a1 onBlur="allowonlythesenumbers(this)">
I can show you the regex version if you'd like

P.S. nice borgtex ;)

Roelf
11-20-2002, 08:04 PM
your logic allows 1300, the specification doesn't :D

Borgtex
11-20-2002, 08:09 PM
hummm your function accepts 1300, that seems to be a not allowed number. Maybe it's just that ksridhar69 forgot it, and then my function it's more silly than it was at beginning ;)

beetle
11-20-2002, 09:02 PM
doh....not sure if ksridhar69 typoed it, or if I need to put it in...easy enough to add....

if (val >= 300 && val <= 2000 && val != 1300 && val % 100 == 0)

ksridhar69
11-20-2002, 09:21 PM
Thanks to Beetle. It works great. Actually I forget 1300

Vladdy
11-20-2002, 10:57 PM
...hmmm....why don't you spare user the necessity to type two extra zeros:

<p class="pclass"><input name="Hundreds" class="inheritStyle" value="" />00</p>

:D :D

Roelf
11-21-2002, 06:07 AM
why not a dropdown?