Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: numeric values only


Message #1 by "Shaukat Desai" <shaukat.desai@g...> on Sun, 19 Nov 2000 12:44:36 -0000
This is a multi-part message in MIME format.

------=_NextPart_000_01CB_01C052EB.86CF5220
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

hi

that is not a good way of validation. Catching individual keystrokes 
will eat away lots of memory and browser may hang or become very slow

if possible do all validations at the time you are submitting the form. 
if you want an immidiate effect use onBlur event
<input type=3D"text" onBlur=3D"checknumbers(this);">

for allowing only numbers, call the function isNumber() and pass the 
string you want to validate as argument to it. and it will return 
boolean value true or false.

<script language=3D"javascript">
function checknumbers(e)
{
 if (isNumber(e.value) =3D=3D false)
 {
  alert("Please enter a valid integer.");
  e.focus();
  return false;
 }
}

function isNumber(e)
{
 var valids =3D "1234567890"
 for (var i=3D0 ; i < e.length ; i++ )
 {
  if (valids.indexOf(e.charAt(i)) =3D=3D -1 ) return false;
 }
}
</script>

  ----- Original Message -----
  From: Shaukat Desai
  To: javascript
  Sent: Sunday, November 19, 2000 6:14 PM
  Subject: [javascript] numeric values only


  Hi, 

  I have the following script which only allows numeric values, I want 
to be
  able to allow for the delete key as well, can someone advise me on how 
I
  can do this.

  Thanks

  Script:-

  <SCRIPT language=3Djavascript>
  var isIE =3D document.all?true:false;
  var isNS =3D document.layers?true:false;

  function OnlyDigits(e) {
  var bolOnlyDigits =3D true;

  if (isIE) {

  if (window.event.keyCode < 46 || window.event.keyCode > 57)
  {
  window.event.keyCode =3D 0;
  bolOnlyDigits =3D false;
  }

  }

  if (isNS) {
  if (e.which < 46 || e.which > 57) {
  e.which =3D 0;
  bolOnlyDigits =3D false;
  }
  }

  return (bolOnlyDigits);
  }
  </SCRIPT>

  ---
  NEED TECHNICAL TIPS, TOOLS, AND INSIGHTS?  Is FREE okay?
  Visit EarthWeb for the latest in IT Management, Software Development,
  Web Development, Networking & Communications, and Hardware & Systems.  

  Click on http://www.earthweb.com for FREE articles, tutorials,
  and discussions from the experts.
$subst('Email.Unsub')





  Return to Index