当页面提交时,执行相关JS函数检查输入是否合法

当页面提交时,执行相关JS函数检查输入是否合法

关键代码

<form action="tj.php" method="post" onSubmit="return check()" name="free">

完整页面代码

<html>
    <head>
        <title>工作记录</title>
        <script LANGUAGE="javascript">
        function check()
        {
            if(document.free.date.value.length==0){
            alert("时间不能为空!");
            document.free.date.focus();
            return false;
            }
            if(document.free.addr.value.length==0){
            alert("地址不能为空!");
            document.free.addr.focus();
            return false;
            }
            if(document.free.content.value.length==0){
            alert("内容不能为空!");
            document.free.content.select();
            return false;
            }

        }
        </script>
    </head>
    
    <body>
    <div align="center">
        <h4>添加工作记录</h4>
        <form action="tj.php" method="post" onSubmit="return check()" name="free">
        <table border="0">
            <tr>
                <td>时间:</td>    <td><input type="text" name="date" value="<?php echo date('Y-m-d');?>" /></td>    <td>&nbsp;</td>
            </tr>
            <tr>
                <td>地点:</td>    <td><input type="text" name="addr" onMouseOver="this.focus()" /></td>    <td>&nbsp;</td>
            </tr>
            <tr>
                <td>手机:</td>    <td><input type="text" name="tel" /></td>    <td>&nbsp;</td>
            </tr>
            <tr>
                <td>内容:</td>    <td><input type="text" name="content" /></td>    <td>&nbsp;</td>
            </tr>
            <tr>
                <td>备注:</td>    <td><input type="text" name="ps" /></td>    <td>&nbsp;</td>
            </tr>
        </table>
        <input type="submit" value="提交"/><input type="reset" value="清空"/>
        </form>
    </div>
    <body>
</html>

猜你喜欢

转载自www.cnblogs.com/insane/p/12355426.html