Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Number only in a data entry box

Status
Not open for further replies.

nk9100

Technical User
May 13, 2005
67
GB
Hi, there is probably already an FAQ for this...


but I am after some code to ensure that only digits are entered into a box... any ideas?

Life is a journey that always ends up in the same place
 
Hi, there is probably already an FAQ for this...
Then why don't you do some of your own research and check before you fill the forum with your request that someone else do your work for you?
I am after some code to ensure that only digits are entered into a box... any ideas?
I suggest you do some research into regular expressions and onchange.

Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Keep in mind that there are a number of ways to enter text in a box besides typing, like pasting, autocomplete, and dragging. Your code will have to account for these methods and possibly others.

It might be easier to allow the user to enter anything they want, then validate it when they leave the field.

Adam
 
nk9100
Here is the isNum function.
When you do your test, pass the current value of the field to this function and it will return true if it is a number and false if not.

function isNum(passedVal) // is passed value a number?
{
if (passedVal == '')
{
return false;
}
for (i=0; i<passedVal.length; i++)
{
if (passedVal.charAt(i) < '0')
return false;
if (passedVal.charAt(i) > '9')
return false;
}
return true;
}
 
<HTML>
<HEAD>
<TITLE>test </TITLE>

<script>
function validateField()
{
var fldValue = document.getElementById("inputNum").value;
if (isNaN(fldValue))
{
alert("The value must be numeric!!!!!!!!");
return false;
}
return true;
}

</SCRIPT>
</HEAD>
<FORM name="test" id="test" ACTION="" METHOD="POST">
Enter a number: <input type="text" name="inputNum" id="inputNum" value="">
<br><br>
<input type="submit" name="butn" value="Submit" onClick="return validateField();">
</form>
</HTML>
 
Hi, I actually used an OnKeyUp method which serves my purpose, thanks to everyone, I apologise for posting the question, but sometimes, we all just think as we type!, thanks to you all.

Life is a journey that always ends up in the same place
 
You may also want to try the isNaN() function ....isNaN means is Not a Number.
Av fun :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top