Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9255039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:37:21+00:00 2026-06-18T11:37:21+00:00

This question may also be titled: Use javascript to set field required? <form class=theform

  • 0

This question may also be titled: “Use javascript to set field required?”

<form class="theform" action="#" method="post" name="theform">
    <label for="tname">Team Name:</label>
    <input type="text" placeholder="Bears" required /><br>
    <label for="captain">Captain:</label>
    <input type="text" placeholder="Captain's Name" required /><br>
    <button type="submit" value="Submit" onclick="toggleReq()" >Submit</button>
</form>

This is a highly simplified version of my form. I know this may seem odd, but I want the form to actually submit with the “Captain” field empty and set as required.

I was thinking that this might–just might–be possible by having javascript remove the required attribute when the submit button is clicked–hence the onclick="toggleReq()".


Since everyone wants to know why this would ever be useful, I’ll explain:
The concept was introduced some time ago that spammers could be foiled by including an extra form field that users were instructed to leave empty. (Ideally this field would be hidden; real users don’t need to know it exists.) It would be evident which comments came from spambots because the spambot would have automatically filled the dummy-field.

There are a couple weaknesses to that approach that I am seeking to address. A spambot could potentially ignore a non-required field. However, simply making the dummy-field required would prevent the form from being submitted by real users. So, the best bet at catching bots is to have the input field required but to have that required tag ignored. Ignoring isn’t technically possible, but switching off the required attribute is.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:37:22+00:00Added an answer on June 18, 2026 at 11:37 am

    If you id the captain’s name field it’s pretty easy. Assuming an id of “captain”:

    function toggleReg(){
       $("#captain").removeAttr('required');
    }
    

    Or the non jQuery version:

    function toggleReq() {
       document.getElementById("captain").removeAttribute("required");
    }
    

    Of course, if you want a true “toggle” you will need to read the current state first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may seem an easy question, but not to me, also a search has
This question may be an absolute crap. Can i use the flex 4 sdk
This question may answer itself, but it is also a question of best practices.
This question may seem blindingly obvious and I realise I am putting myself up
This question may seem naive. I don't want to reach the power of this
This question may seem trivial, but I hope you won't ignore it. Before destroying
This question may seem a little bit stackoverflow-implementation specific, but I have seen a
This question may have been asked before, but I had trouble finding an answer,
This question may not directly relate to programming. I have noticed that the technology
This question may seem daft (I'm a new to 'programming' and should probably stop

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.