js校驗表單後提交表單的三種方法總結

本篇文章主要是對js校驗表單後提交表單的三種方法進行了總結介紹,需要的朋友可以過來參考下,希望對大家有所幫助

第一種

<script type="text/javascript">
    function check(form) {
        if (form.userId.value == '') {
            alert("請輸入用戶帳號!");
            form.userId.focus();
            return false;
        }
        if (form.password.value == '') {
            alert("請輸入登錄密碼!");
            form.password.focus();
            return false;
        }
        return true;
    }
</script>

<form action="login.do?act=login" method="post">
    用戶帳號
    <input type=text name="userId" size="18" value="">
    <br>
    登錄密碼
    <input type="password" name="password" size="19" value=""/>
    <input type=submit name="submit1" value="登陸" οnclick="return check(this.form)">
</form>

第二種

<script type="text/javascript">
    function check(form) {
        if (form.userId.value == '') {
            alert("請輸入用戶帳號!");
            form.userId.focus();
            return false;
        }
        if (form.password.value == '') {
            alert("請輸入登錄密碼!");
            form.password.focus();
            return false;
        }
        return true;
    }
</script>

<form action="login.do?act=login" method="post" οnsubmit="return check(this)">
    用戶帳號
    <input type=text name="userId" size="18" value="">
    <br>
    登錄密碼
    <input type="password" name="password" size="19" value=""/>
    <input type=submit name="submit1" value="登陸">
</form>

第三種

<script type="text/javascript">
    function check(form) {
        if (form.userId.value == '') {
            alert("請輸入用戶帳號!");
            form.userId.focus();
            return false;
        }
        if (form.password.value == '') {
            alert("請輸入登錄密碼!");
            form.password.focus();
            return false;
        }
        document.myform.submit();
    }
</script>

<form action="login.do?act=login" name="myform" method="post">
    用戶帳號
    <input type=text name="userId" size="18" value="">
    <br>
    登錄密碼
    <input type="password" name="password" size="19" value=""/>
    <input type=button name="submit1" value="登陸" οnclick="check(this.form)">
</form>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章