0

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>
5
  • Select elements using $(":input[required]") Commented Apr 14, 2016 at 8:32
  • Can you show me this in my if statement please? im really struggling Commented Apr 14, 2016 at 8:35
  • Can you show us complete example ? Commented Apr 14, 2016 at 8:36
  • No really sorry its to large :( that is just the outer IF then all the validation happens inside. I am using a $.each to loop through all the fields FYI Commented Apr 14, 2016 at 8:39
  • "I want to validate fields with a data type and the required attribute. Or if a field is not null?" that literally doesn't mean anything. Commented Apr 14, 2016 at 8:44

1 Answer 1

1

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

if ($(this).attr("data-type") != "" && $(this).prop("required") != false && $(this).val()!="") {}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.