258

I have been searching ways to have jQuery automatically write required using html5 validation to my all of my input fields but I am having trouble telling it where to write it.

I want to take this

 <input type="text" name="first_name" value="" id="freeform_first_name"
 maxlength="150">

and have it automatically add required before the closing tag

 <input type="text" name="first_name" value="" id="freeform_first_name"
 maxlength="150" required>

I thought I could do someting along the lines of

$("input").attr("required", "true");

But it doesn't work. Any help is greatly appreciated.

4
  • 2
    Is it not wrapped in dom ready? $(function(){....}); Commented Oct 3, 2013 at 18:40
  • I am using this jsfiddle.net/japaneselanguagefriend/LEZ4r Commented Oct 3, 2013 at 18:44
  • if 'input' is an id the jquery is missing the # in the selector here. Commented May 11, 2016 at 20:43
  • @JohnMeyer "input" is a tag selector. No # needed if targeting input tags Commented May 24, 2017 at 17:45

6 Answers 6

562
$("input").prop('required',true);

DEMO FIDDLE

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

5 Comments

Hmm, added a submit button but still not validating or adding the required attribute to the input fields. jsfiddle.net/japaneselanguagefriend/LEZ4r
@JohnMeyer "input" is a tag selector
got a TypeError: element.prop is not a function error
@Miura-shi You're missing the <form></form> tags in your jsFiddle
If it can help someone : this wasn't working for me at first because I was calling .prop('required',true) on the input before calling .css("display", "block") on it.
71

You can do it by using attr, the mistake that you made is that you put the true inside quotes. instead of that try this:

$("input").attr("required", true);

3 Comments

@JohnMeyer "input" is the name of a tag selector
required is a boolean attribute and should only ever be omitted (for "false"), or have the same value as its name (i.e. "required") for "true". It's actually better to use .prop().
@Alnitak Not if you're working on making a site compatible with IE, prop() doesn't work on there as it does on modern browsers. Still best to use $(ele).attr("required", true) and false to remove.
47

I have found that the following implementations are effective:

$('#freeform_first_name').removeAttr('required');

$('#freeform_first_name').attr('required', 'required');

These commands (attr, removeAttr, prop) behave differently depending on the version of JQuery you are using. Please reference the documentation here: https://api.jquery.com/attr/

Comments

22

Using .attr method

.attr(attribute,value); // syntax

.attr("required", true);
// output: required="required"

.attr("required", false);
// output:

Using .prop

.prop(property,value) // syntax

.prop("required", true);
// output: required=""

.prop("required", false);
// output: 

Read more from here

https://stackoverflow.com/a/5876747/5413283

Comments

9

Should not enclose true with double quote " " it should be like

$(document).ready(function() {            
   $('input').attr('required', true);   
});

Also you can use prop

jQuery(document).ready(function() {            
   $('input').prop('required', true);   
}); 

Instead of true you can try required. Such as

$('input').prop('required', 'required');

Comments

6

I found that jquery 1.11.1 does not do this reliably.

I used $('#estimate').attr('required', true) and $('#estimate').removeAttr('required').

Removing required was not reliable. It would sometimes leave the required attribute without value. Since required is a boolean attibute, its mere presence, without value, is seen by the browser as true.

This bug was intermittent, and I got tired of messing with it. Switched to document.getElementById("estimate").required = true and document.getElementById("estimate").required = false.

1 Comment

removeAttr doesn't work to me. Then I use your solution and works fine. Thanks!

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.