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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ensure that the user enters only digits

Validation

Ensure that the user enters only digits

by  erayoral  Posted    (Edited  )
For example, we have a text field and want users to enter
only digits.

The function is triggered with "onKeyUp" action and starts
to check the characters if they are digits or not.As soon as
it finds a non-digit character, it removes the rest of the text and updates the text field's value.

Code:
<html>

<head>

<script language="JavaScript">

  function numericKontrol( alan )  {

    var msg=alan.value;
    var chr;

    for ( i=0; i < msg.length; i++ )  {
      chr=msg.substring(i,i+1);
      if ( chr<"0" || chr>"9" )  {
	msg=msg.substring(0,i);
	alan.value=msg;
      }
    }
  }

</script>

<title>Deneme</title>

</head>

<body>

<form name="form1" method="post" action="gonder.html">

  <input name="Txt01" type="text" value=""
    size="30" onKeyUp="numericKontrol(this)"><br>

  <input type="button" value="Gonder">
  <input type="Reset" value="Temizle">

</form>

</body>

</html>

Hope this helps somebody!
Eray Oral ( marmara )
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top