利用javascirpt 限制只能輸入數字 使用ASCII碼

摘要:利用javascirpt 限制只能輸入數字 使用ASCII碼

------------------------------------------------  Input 控制項作法  ------------------------------------------------------

<script language="JavaScript" type="text/javascript">
    function CheckKey(key) {
        if (key >= 48 && key <= 57)
         { return true; }
         else { return false; }
    }
 </script>

  <input type="text" name="birthday"  onkeypress="return CheckKey(event.keyCode)"  />

-------------------------------------------    Asp.net  利用 cs 程式碼去操控     -----------------------------------------------

    protected void LoadScript()
    {

        StringBuilder oSB = new StringBuilder();
        oSB.AppendFormat("document.getElementById('{0}').onkeydown = function()", txtGoPage.ClientID);
        oSB.Append("{");
        oSB.Append("if(event.keyCode>47 && event.keyCode<58 ){");
        oSB.Append("return true;");
        oSB.Append("}else{");
        oSB.Append("return false;}");
        oSB.Append("};");

        if (this.Page.ClientScript.IsStartupScriptRegistered(this.GetType(), "jsPage") == false)
        {
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "jsPage", oSB.ToString(), true);
        }
    }