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 a valid decimal number

Validation

Ensure that the user enters a valid decimal number

by  erayoral  Posted    (Edited  )
For example, we have a text field that must include only decimal numbers ( numbers containing "." ). If the user enters a number without a dot ( "." ), it is ok. But if
he/she enters one with a dot ( e.g. 1212.??? ), we must prevent the user from entering a second dot.

Here is the code:

If the user enters non-digit characters, they are removed.
Also a dot at the beginning is not allowed, while a dot
at the end is OK. As soon as a second dot is entered, it is
removed.

Code:
<html>

<head>

<script language="JavaScript">

  function ondalikSayiKontrol( alan ) {

    var msg=alan.value; 
    var w;
    var nokta=msg.indexOf( "." );
    var ind;
  
    for ( w=0; w<msg.length; w++ )  {

      ind=msg.substring(w,w+1);
      if ( ind<"0" || ind>"9" )  {
    
	       if ( nokta>0 )
	         if ( w==nokta )  continue;    
       
	       msg=msg.substring(0,w);
	       alan.value=msg; 
	       break;           
      }

    }

  }

</script>

<title>Deneme</title>

</head>

<body>

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

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

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

</form>

</body>

</html>

Hope this is helpful!
Please make us aware for better solutions!
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