Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何验证表单是否为空?PHP或JS_Php_Javascript_Jquery_Html_Forms - Fatal编程技术网

如何验证表单是否为空?PHP或JS

如何验证表单是否为空?PHP或JS,php,javascript,jquery,html,forms,Php,Javascript,Jquery,Html,Forms,请注意,我是PHP新手,创建了我的网站,使用文本表单,我可以使用JQuery验证txt表单是否为空,并使用以下代码: $(document).ready(function() { $('#formLike').ajaxForm({ target: '#content', beforeSubmit: validateForm

请注意,我是PHP新手,创建了我的网站,使用文本表单,我可以使用JQuery验证txt表单是否为空,并使用以下代码:

    $(document).ready(function()
    {
        $('#formLike').ajaxForm({                       
                target:         '#content',
                beforeSubmit:   validateForm
        }); 

        //hide error containers
        $("#txtform_error").hide();

    });

    function validateForm()
    {
        $("#txtform_error").empty().hide();

        var txtform         = $("#txtform").val();
        var errors              = 0;

        if (txtform == null || txtform == ''  || txtform == ' ')
        {
            $("#txtform_error").show().append("Please Write a Message Before Clicking : Like It");
            document.formLike.txtform.focus();
        }
        else
        {
        document.formLike.submit();
        }


    }
如何验证txt表单不是空的,并且字段中不仅有空格,因为在这里提交了许多空格后,文本将发布到操作页面

对不起,我的英语不好


提前感谢。

替换此行

if (txtform == null || txtform == ''  || txtform == ' ')
if ($.trim(txtform) == "")
使用此行

if (txtform == null || txtform == ''  || txtform == ' ')
if ($.trim(txtform) == "")
$。trim
删除不必要的空格,因此如果有人只输入空格,它将被修剪为“”

检查:

if (txtform == null || txtform == ''  || txtform == ' ')
一定是

if (!txtform || txtform.replace(/\s/g, '') == '') 


这意味着,输入必须至少包含1个非空格字符

最好在第二次检查中使用
if(!/\S/.match(txtform))
,而不是执行替换操作。我已删除了答案的第一部分,因为val()实际上是正确的。替换和匹配方法不起作用,Google Chrome JS控制台返回:Uncaught TypeError:Object/\S/没有“匹配”方法。您的函数将删除所有空格,而不仅仅是前导空格和尾随空格。应该使用以下内容:
if(txtform.replace(/^\s+/g,”).replace(/\s+$/g)=='')
.val()
总是返回一个字符串,所以检查null是愚蠢的)jQuery.trim(str)已经这么做了。删除所有空格是可以的,因为如果输入中没有非空格字符,结果将为真。所以如果我们说,“a”=假。“a”=假。“a”=假。“”=正确。“”=正确。“”=真