JIMMY THE CODE
JIMMY THE CODE

Reputation: 159

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

I am doing some validation and can't get my if statement to work. I want to validate fields with a data type and the required attribute. Or if a field is not null?

My if statement:

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

The HTML input:

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

Upvotes: 0

Views: 97

Answers (1)

Parth Jasani
Parth Jasani

Reputation: 2449

There is no require typeof. try this query ...

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

Upvotes: 1

Related Questions