2

What is the best JQuery way to ensure that all input fields with the attribute "required" are completed

10 Answers 10

3

Use the .filter() method to check the value of each and then check the length property of the result:

var incomplete = $("input[required]").filter(function () {
    return this.value === "";
});
if (incomplete.length) {
    alert("Please fill in all the required fields");
}

Note that an attribute named required will invalidate your HTML. data-required would be valid HTML5, however, or you could just give them all a class and use that as the selector (which would also be more efficient).

You could also use one of the validation plugins out there: http://google.com/search?q=jquery+validation.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Andy thanks for this, you are right about HTML, but this is for HTML5 :)
@Russell: then you should use data-required for valid HTML5, without data- it's still invalid ;-)
1

The best JQuery validation plugin is: http://bassistance.de/jquery-plugins/jquery-plugin-validation/

(At least the one I use and feel most comfortable with)

Comments

0

You can try to use jquery validation plugin:

http://docs.jquery.com/Plugins/validation

Comments

0

Try this:

http://speckyboy.com/2009/12/17/10-useful-jquery-form-validation-techniques-and-tutorials-2/

Comments

0

For reference : jQuery Validation

For Example : webreference

Comments

0

There is also a plugin called 'valididty' that is worth a mention and is simpler to use IMO that the validation plugin.

http://validity.thatscaptaintoyou.com/

Comments

0

You can also try bValidator plugin (http://code.google.com/p/bvalidator/) for form validation

Comments

0

Just add two attributes at the end of your input tags:
required="required" js-name="name_to_be_displayed"

<script language="javascript" src="jquery.min.js"></script>
<script language="javascript" type="application/javascript">
$(function (){
    $("#submit").click(function (){
        $('input').each(function(index, value) {
            var attb = $(this).attr('required');
            var value = $(this).val();

            if (attb == "required" && value == "") {
                alert($(this).attr('js-name') + " " + 'value required');
                $(this).focus();
                return false;
            }
        });
    });
});
</script>
<form action="index.php" method="post" enctype="multipart/form-data" target="_self">
<label> Name:   
<input name="Name:" type="text" size="60" maxlength="60" required="required" js-name="Name" />
</label>
<br />
<label>Email:
<input name="Email:" type="email" value="" size="60" required="required" js-name="Email" />
</label>
<br />
<label>Phone:
<input name="Phone" type="text" size="60" required="required" js-name="Phone" /> 
</label>
<br />
<label>Country:
<input name="Country" type="text" size="60" required="required" js-name="Country" />
</label>
<br />
<input name="Submit" type="submit" id="submit" value="Submit" />
</form>

Comments

0

Check this simple jQuery plugin

http://suraj-mahajan.github.io/Validate-In-Grid-Controls/

Comments

0

valijate plugin might help you. it uses 'data-xxxx' attributes for the validations. here is form validation guide.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.