1

I need CSS code to restrict submit button if fields are empty.Daily we are receiving 3-5 blank inquiries through our WordPress landing page submit button.

Where to put these CSS codes if any.

Thanks

3
  • 1
    well, first off, generally such things are done in javascript instead of in CSS. Depending on the exact (html) structure of the form though, it may (only in certain cases) be possible to hide the submit button (while the textbox value="") in CSS. -- Generally an even better approach (in addition to the javascript) is to filter the submission serverside in PHP for example. Commented Dec 19, 2021 at 12:37
  • Thanks. I was wondering if we can do it in CSS aswell Commented Dec 19, 2021 at 12:50
  • Its not super likely, but if the text input and the submit input happen to be siblings in the html code , then it is possible using the css + selector. Commented Dec 19, 2021 at 13:02

1 Answer 1

2

You really should do this with a script, because doing something like this by CSS is very sensitive to any future changes to your form structure. It can be done with only CSS, using the :placeholder-shown selector. For this you'll need to add a placeholder to all text inputs.

/* As long as one of the selectors is matched, the button won't show. */
form input:placeholder-shown ~ button,
form textarea:placeholder-shown ~ button {
  display: none;
}
<form>
first name: <input type="text" name="firstname" placeholder="Enter first name"><br>
Last name: <input type="text" name="lastname" placeholder="Enter last name"><br>
Text area<br>
<textarea name="textarea" placeholder="Enter some text..."></textarea>
<br>
<button type="submit">Submit</button>
</form>

This will work, but for any change in the form you'll need to make sure it doesn't break. I personally won't use this :)

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.