如何验证具有必需属性或不为null的字段

How do I validate fields with the required attribute or not null?

本文关键字:属性 null 字段 何验证 验证      更新时间:2024-06-27

我正在进行一些验证,无法使if语句正常工作。我想验证具有数据类型和必需属性的字段。或者字段不为空?

我的if语句:

if (typeof $(this).attr("data-type") != "undefined" 
        && typeof $(this).prop("required") != "undefined"
        || typeof $(this).prop("required") === true
        || $(this).val() != "") {
    // Doing stuff
}

HTML输入:

<input type="text" name="Consignee" id="text" data-type="text"
       placeholder="Consignee" required>

没有必需的typeof。尝试此查询。。。

if ($(this).attr("data-type") != "" && $(this).prop("required") != false && $(this).val()!="") {}