gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 20:33:19 24 4
gpt4 key购买 nike

拜托,我是 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) == "")

$.trim 删除不必要的空格,因此如果有人仅输入空格,它将被 trim 为“”。

关于php - 如何验证表单是否为空? PHP 或 JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3714633/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com