javascript小執行個體(判斷使用者輸入的是不是數字,截取兩位,四捨五入)

來源:互聯網
上載者:User

參考文章:http://topic.csdn.net/t/20060907/19/5006672.html

090103檢查是否是數字並且大於零

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="test_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5.     <title>檢查是否是數字並且大於零</title>
  6.     <script type="text/javascript">
  7.         //判斷函數。
  8.         function fCheckNumById(id){
  9.             var txt = document.getElementById(id);
  10.             if(!isNaN(txt.value)&&txt.value>0)//如果是數字則返回false,等於零也通過不了。
  11.                 return true;
  12.             else
  13.             {
  14.                 return false;
  15.             }
  16.         }
  17.         //調用判斷。
  18.         function fCheck(){
  19.             if(fCheckNumById('tb1'))
  20.             {
  21.                 //執行業務
  22.                 alert('通過驗證');
  23.                 return true;
  24.             }
  25.             else 
  26.             {
  27.                 alert('非數字或小於0!');
  28.                 return false;
  29.             }
  30.         }
  31.     </script>
  32. </head>
  33. <body>
  34.     <form id="form1" runat="server">
  35.     <div>
  36.         <asp:TextBox ID="tb1" runat="server"></asp:TextBox>
  37.         <input type="button" onclick="fCheck();" value="check" />
  38.     </div>
  39.     </form>
  40. </body>
  41. </html>

前台

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="test1.aspx.cs" Inherits="xcgl_test1" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5.     <title>調用用戶端代碼判斷輸入是否合格</title>
  6.     <script type="text/javascript">
  7.     function fCheck(){
  8.         var txt = document.getElementById('tbEnter');
  9.         if(txt.value=='')
  10.         {
  11.             alert('不可為空白!');
  12.             return false;
  13.         }
  14.         else
  15.         {
  16.             if(isNaN(txt.value))//判斷是否是非數字
  17.             {
  18.                 alert('不可為非數字!');
  19.                 return false;
  20.             }
  21.             else
  22.             {
  23.                 alert('提示,小數倍數只保留兩位。');
  24.                 return true;
  25.             }
  26.         }
  27.     }
  28.     </script>
  29. </head>
  30. <body>
  31.     <form id="form1" runat="server">
  32.         <div>
  33.             <asp:TextBox ID="tbEnter" runat="server"></asp:TextBox>
  34.             <asp:Button ID="btnCheck" runat="server" Text="用戶端判斷成功才執行伺服器代碼" OnClick="btnCheck_Click"
  35.                 OnClientClick="return fCheck();" />
  36.         </div>
  37.     </form>
  38. </body>
  39. </html>

後台

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. public partial class xcgl_test1 : System.Web.UI.Page
  12. {
  13.     protected void Page_Load(object sender, EventArgs e)
  14.     {
  15.     }
  16.     protected void btnCheck_Click(object sender, EventArgs e)
  17.     {
  18.         //取兩位小數,但不四捨五入。
  19.         double d = Convert.ToDouble(tbEnter.Text.Trim());
  20.         double i = (double)((int)(d * 100)) / 100;
  21.         //四捨五入
  22.         string str = d.ToString("0.##");
  23.         Response.Write(i + "||" + str);
  24.     }
  25. }

end

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.