用 javastript 驗證 asp.net 頁面

來源:互聯網
上載者:User

//按扭驗證 如果在asp.net 上
   string onclick_button1 = string.Empty;
   onclick_button1 += "if(yz_t1()==false){return false;}";
   onclick_button1 += "if(yz_date()==false){return false;}";
   onclick_button1 += "if(yz_t6()==false){return false;}";
   this.Button2.Attributes.Add("onclick",onclick_button1);

 //驗證-t1
function yz_t1()
{
 if(document.forms(0).TextBox1.value=='')
 {
  alert('請您輸入姓名!');
  document.forms(0).TextBox1.focus();
  return false;
 }
}

//驗證-c1
function yz_c1()
{
 for(var i=1;i<127;++i)
 {
  var tbi=document.getElementById("Textbox"+i).value;
  if(tbi!="")
  {
   if(tbi.search("^[0-9]*[0-9][0-9]*$") == -1)
   {
    alert('所有輸入都為正整數,請重新輸入!');
    document.getElementById("Textbox"+i).focus();
    return false;
   }
  }
 }
}

function a2()
{
    var id=Form1.y1.value;
 if (id.search("^[A-Za-z0-9_]+$") == -1)
 {
  alert("使用者名稱由數字、26個英文字母或者底線組成,請正確填寫。");
  return false;
 }
}

function a3()
{
    var id=Form1.y2.value;
 if (id.search("^[A-Za-z0-9_]+$") == -1)
 {
  alert("使用者名稱由數字。");
  return false;
 }
}

//時間
function yz_date()
{
  str=document.forms(0).TextBox2.value;
  if (str=="")
  {
    return true;    
  }
     var re=/^/d{4}-/d{1,2}-/d{1,2}$/;
  var r=str.match(re);
  if (r==null)
  {
    alert("填表時間,請按以下格式填寫日期: yyyy-mm-dd!");
    document.forms(0).TextBox2.focus();
    return false;    
  }
  else
  {
    var s=str.split("-");
    var years = parseInt(s[0],10);
    var months = parseInt(s[1],10);
    var days = parseInt(s[2],10);
    if (parseInt(s[0].substring(0,2))<19 || months>12 || months<1 || days>31 || days<1)
    {
      alert("填表時間,請填寫正確的日期");
      document.forms(0).TextBox2.focus();
      return false;
    }      
   
    switch(months)
    {
      case 2:
        if((years%4 == 0 && years%100 != 0) || (years%400 == 0))
        {
          if(days > 29)
          {
            alert("填表時間,閏年二月沒有"+days+"日!");
            document.forms(0).TextBox2.focus();
            return false;
          }                
        }
        else
        {
          if(days > 28)
          {
            alert("填表時間,平年二月沒有"+days+"日!");
            document.forms(0).TextBox2.focus();
            return false;
          }
        }
        break;
     
      case 4:
        if(days > 30)
        {
          alert("填表時間,四月沒有"+days+"日!");
          document.forms(0).TextBox2.focus();
          return false;
        }
        break;
      case 6:
        if(days > 30)
        {
          alert("填表時間,六月沒有"+days+"日!");
          document.forms(0).TextBox2.focus();
          return false;
        }
        break;
      case 9:
        if(days > 30)
        {
          alert("填表時間,九月沒有"+days+"日!");
          document.forms(0).TextBox2.focus();
          return false;
        }
        break;
      case 1:
        if(days > 30)
        {
          alert("填表時間,十一月沒有"+days+"日!");
          document.forms(0).TextBox2.focus();
          return false;
        }
        break;
    }
    return true;
    }
 }

聯繫我們

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