Javascript 表單提交前驗證執行個體代碼

來源:互聯網
上載者:User

例

給表單的加上onsubmit屬性或者提交按鈕的加上onclick屬性,值為return check(),check是javascript函數,驗證表單用的,沒有return,即使驗證失敗也會提交。在check函數中,驗證失敗返回false,成功返回true.
代碼:

 代碼如下 複製代碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>無標題文檔</title>
    </head>
    <body>
     <form action="index.php" method="get" name="form1">
      姓名:<input type="text" name="name" id="name" /><br/>
   年齡:<input  type="text" name="age" id="age" /><br/>
   <input type="submit" onclick="return check()" />   
 
     </form>
        <script language="javascript" type="text/javascript">
    function check(){
     var age=parseInt(document.form1.age.value);
     if(age<18){
      alert("小於18");
      return false;
     }else{
      return true;
     }
    }
        </script>
    </body>
</html>

例2

利用jquery 表單驗證外掛程式validator

調用簡單,樣式通用,你不用再寫樣式。一款非常好用的網頁表單驗證控制項。


先看看效果吧,如下圖:

 代碼如下 複製代碼


<SCRIPT type=text/javascript src="js/validator.js"></SCRIPT>
<SCRIPT type=text/javascript>  
    var a = validator({
        "form": "validator-form",
        ajaxSubmit:true,
        beforSubmit:function(){
            //alert("表單提交前執行的函數");
        },
        afterSubmit:function(){
            alert("ajax提交後返回的資料是:"+this.responseText);
        }
    }).add({
        "target": "username",
        "rule_type": "username",
        'action':'2.php',
        'tips':'說點什麼好呢'
    }).add({
        "target": "password",
        "rule_type": "password"
    }).add({
        "target": "confirm-password",
        "rule_type": "password",
        "sameTo": "password",
        'error':'你填寫的密碼不正確或者和原密碼不同'
    }).add({
        "target": "age",
        "rule_type": "number||empty",
        "error":'雖然這玩意兒不是必填項,但既然你填了,就把它填對吧。'
    }).add({
        "target": "birthday",
        "rule_type": "date||empty"
    }).add({
        "target": "id-card",
        "rule_type": "idcard||empty"
    }).add({
        "target": "email",
        "rule_type": "email"
    }).add({
        "target": "qq",
        "rule_type": "qq||empty"
    }).add({
        "target": "telephone",
        "rule_type": "phone||empty"
    }).add({
        "target": "mobile",
        "rule_type": "mobile||empty"
    }).add({
        "target": "phone",
        "rule_type": "phone||mobile",
        "tips": "請輸入您的手機或有線電話電話號碼!"
    }).add({
        "target": "zip-code",
        "rule_type": "zip||empty"
    }).add({
        "target": "ip-address",
        "rule_type": "ip||empty"
    }).add({
        "target": "url",
        "rule_type": "url||empty"
    }).add({
        "target": "message",
        "rule_type": "require"
    });
 
    var reset = document.getElementById('reset');
    var del = document.getElementById('del');
    reset.onclick = function(){
        a.reset();
    }
    del.onclick = function(){
        a.remove('username');
    }
 
</SCRIPT>

聯繫我們

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