-2

I would like to implement in html a mask for a text input field which accepts only a number with option for 3 digits after the point (3 digit are only optional for the user).

is it possible to implement without javascript? i have no idea how to implement this code.

shiran
  • 13
  • 2

1 Answers1

0
<!-- language: html -->    
<!DOCTYPE html>
<html>
<head>
<html> 
<head>   
<script>
  // Retrieve last key pressed.  Works in IE and Netscape.
  // Returns the numeric key code for the key pressed.
  function getKey(e)
  {
    if (window.event)
       return window.event.keyCode;
    else if (e)
       return e.which;
    else
       return null;
  }
  function restrictChars(e, obj)
  {
    var CHAR_AFTER_DP = 3;  // number of decimal places
    var validList = "0123456789.";  // allowed characters in field
    var key, keyChar;
    key = getKey(e);
    if (key == null) return true;
    // control keys
    // null, backspace, tab, carriage return, escape
    if ( key==0 || key==8 || key==9 || key==13 || key==27 )
       return true;
    // get character
    keyChar = String.fromCharCode(key);
    // check valid characters
    if (validList.indexOf(keyChar) != -1)
    {
      // check for existing decimal point
      var dp = 0;
      if( (dp = obj.value.indexOf( ".")) > -1)
      {
        if( keyChar == ".")
          return false;  // only one allowed
        else
        {
          // room for more after decimal point?
          if( obj.value.length - dp <= CHAR_AFTER_DP)
            return true;
        }
      }
      else return true;
    }
    // not a valid character
    return false;
  }
</script>
</head>
<body>
<form name="test">
  Input Number <input type="text" name="myNum" onKeyPress="return restrictChars(event, this)">
</form>
</body>

This will restrict the decimal places to three however it still needs validation and this is just for positive numbers.

Brian
  • 36
  • 3