如何检查文本框是否为空? [英] How to check whether a text box is empty or not?

查看:87
本文介绍了如何检查文本框是否为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查文本框是否包含名称。如果没有,则在按下提交按钮后应弹出警告,显示消息,并且页面不应提交空白值。如果它包含值,则应提交该值。

I want to check whether a text box contains a name or not. If not then an alert should be popped up displaying a message after pressing a submit button and the page should not submit the blank value. If it contains value then the value should be submitted.

我使用以下代码。当我将文本框留空并单击提交按钮时,它会按预期显示警报,但在解除警报后也会提交空值。

I am using the below code. When I leave the text box empty and click the submit button, it displays the alert as expected, but it also submits the empty value after dismissing the alert.

<html>
    <head>
        <script type="text/javascript">

            function check()
            {
                if (!frm1.FileName.value)
                {
                    alert ("Please Enter a File Name");
                    return (false);
                }
                return (true);
            }

        </script>
    </head>
    <body>
        <form name="frm1" id="frm1" action="/cgi-bin/page.pl" method="POST">
            <input type="text" name="FileName" id="FileName">       
            <input type="submit" value="send" name="btn_move" id="btn_move" onclick="check()">
        </form>
    </body>
</html>

代码中有什么问题?

推荐答案

你需要做 onclick =return check();

这篇关于如何检查文本框是否为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆