利用javascript指令碼實現簡單的驗證

來源:互聯網
上載者:User

今天在購書網的圖書管理頁面遇到一個小問題,關於使用者輸入驗證:使用.NET的驗證控制項可以實現較多的使用者輸入驗證,但提示資訊輸出不是很靈活,所以嘗試在頁面較為顯眼的地方使用DIV標記顯示錯誤資訊.頁面表單為3個TextBox控制項及一個ImageButton控制項,javascript代碼如下:
因使用了主版頁面,在VS2005編輯的環境下控制項的ID並不是真正的ID,在輸出為HTML代碼後會在該ID前自動添加一些字元,故需要在瀏覽器中將控制項的ID複製下來,得到的才是真正的該控制項ID

<script language=javascript>
var flag=false;
function check(obj,str)
{
    var o=document.getElementById(obj);
    if(o.value=="")
    {
        document.getElementById("bookname").innerText=str;
       
    }
    else
    {
        document.getElementById("bookname").innerText="";
        flag=true;
     }
     if(flag)
     {
         //ImageButton2的disabled預設設定為disabled;
        document.getElementById("ctl00_ContentPlaceHolder1_ImageButton2").disabled=false;
      }
 }
</script>

頁面CS檔案代碼如下:
protected void Page_Load(object sender, EventArgs e)
    {
        TextBox1.Attributes.Add("onBlur", "return check('"+TextBox1.ClientID +"','作者為空白!');");
        TextBox2.Attributes.Add("onBlur", "return check('" + TextBox2.ClientID + "','書名為空白!');");
        TextBox3.Attributes.Add("onBlur", "return check('" + TextBox3.ClientID + "','出版社為空白!');");
    }

相關文章

聯繫我們

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