javascript讓文字框只能輸入數字, 帶數字類型和限制最大值功能.

來源:互聯網
上載者:User

 網上看到過很多javascript驗證數字輸入的方法, 自己也寫了個算是總結吧. 如有演算法和邏輯上的問題, 歡迎大家拍磚啊.
IE6.0下測試通過.

轉載請註明出自: http://blog.csdn.net/pisces_fri/

<SCRIPT language="javascript" type="text/javascript">
/**
* @author: pisces.fri
*/

 

// =============================================================================
// validationNumber methods

/**
* Validate the input text as a number
*
* @param object ID of the input control object
* @param string Number type ('int', 'u_int', 'float' or 'u_float')
* @param number Max number allowed to input
* @param object ID of the error message control object
*
* @return null
*/
function validationNumber(hndID, numType, maxNum, hndMsgID)
{
 var keyCode = window.event.keyCode;
 var ch = String.fromCharCode(keyCode);
 var value = '';
 var retCode = 0;
 var oNumVerify=null;

 //屏蔽斷行符號鍵
 if (keyCode==13)
 {
  window.event.keyCode = 0;
  return null;
 }

 if (hndID != undefined)
 {
  value += hndID.value+ch;
 }
 else
 {
  window.event.keyCode = keyCode;
  return null;
 }

 oNumVerify = new isNumeric(value);
 if (oNumVerify.isNumber)
 {
  numType = numType.toString().toLowerCase();
  switch(numType)
  {
   case 'u_int':  //正整數
    if ((oNumVerify.isMinus==false) && (oNumVerify.isDecimal==false))
    {
     retCode = keyCode;
    }
    break;
   case 'u_float':  //正實數
    if (oNumVerify.isMinus==false)
    {
     retCode = keyCode;
    }
    break;
   case 'int':   //整數
    if (oNumVerify.isDecimal==false)
    {
     retCode = keyCode;
    }
    break;
   case 'float':  //實數
    retCode = keyCode;
    break;
   default:
    retCode = 0;
  }

  //判斷輸入的數字是否超過設定的最大值
  if ((maxNum!=undefined) && (maxNum.constructor==Number) && (oNumVerify.value > maxNum))
  {
   retCode = 0;
   if (hndMsgID != undefined)
   {
    hndMsgID.innerHTML = '<SPAN style="color:#EE3333">值不能大於'+maxNum+'</SPAN>';
   }
  }
 }

 oNumVerify = null;
 window.event.keyCode = retCode;
 return null;
}

function isNumeric(verifyNum)
{
 var re = /^([-]{0,1})([0-9]*)([/.]{0,1})([0-9]*)$/g;
 this.isNumber = false;
 this.isMinus = false;
 this.isDecimal = false;
 this.value = verifyNum;

 verifyNum = verifyNum.toString();

 if (re.test(verifyNum))
 {
  this.isNumber = true;
  re.exec(verifyNum);

  //判斷 '-' 符號
  if (RegExp.$1=='-')
  {
   this.isMinus = true;
  }

  //判斷 '.' 符號
  if (RegExp.$3=='.')
  {
   this.isDecimal = true;
   verifyNum += '0';
  }

  try
  {
   this.value = parseFloat(verifyNum);
  }
  catch(e)
  {
   this.value = 0;
  }
 }

 return;
}
</SCRIPT>
使用方法:<br/>
<INPUT type="text" name="txtYear" id="txt_Year" size="4" onkeypress="javascript:validationNumber(this, 'float', 2006, txtYearMsg);" style="text-align:right;ime-mode:disabled;" maxlength="4"/>年&nbsp;&nbsp;<SPAN id="txtYearMsg"></SPAN>

 

IE6.0下測試通過.

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.