javascript form check and focus

來源:互聯網
上載者:User

<%@page contentType="text/html;charset=gb2312"%>
<html>
<head>

<script language="javasctipt" type="text/javascript">
  function login(){
   //thisForm是form中的name
 var form = document.thisForm;
 
 if(trim(form.name.value) == ""){
  window.alert("請輸入姓名!");
  form.name.select();//或者form.name.focus()
  return;
 }
 /**或像下面那樣也可以
 if (trim(document.thisForm.username.value)=="")
  {
   alert("請填寫您的姓名!");
   document.thisForm.name.focus();
   return ;//false;
  }
 */
 if(trim(form.password.value) == ""){
  window.alert("請輸入密碼!");
  form.password.select();
  return;
 }

   // 還可以做很多事情,比如在這裡又調用其他的function來觸發其他事件
 
    form.target = "_blank";//彈出新的頁面
    form.action = "isNull.jsp";//form中的action值
    form.method = "post";
    form.submit();//提交form  
  }
  /** 修剪字串前後的空格  */
function trim(s) {
  var count = s.length;
  var st    = 0;       // start
  var end   = count-1; // end

  if (s == "") return s;
  while (st < count) {
    if (s.charAt(st) == " ")
      st ++;
    else
      break;
  }
  while (end > st) {
    if (s.charAt(end) == " ")
      end --;
    else
      break;
  }
  return s.substring(st,end + 1);
}
</script> 
</head>
  <body>
  <form action="" method="post" name="thisForm" id="thisForm">
   <table>
   <tr>
  <td><input type="text" name="name" value="" maxlength="20" style="length:30px;" /></td>
  <td><input type="password" name="password" value="" maxlength="20" style="length:30px;" /></td>
  <td><input type="button" name="btnFast" onclick="login();" value="登入" /></td>
   </tr>
   </table>
  </form>
  </body>
</html>

相關文章

聯繫我們

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