John John - 1 month ago 10
Javascript Question

Make required field prompt come before onclick=confirm() prompt

Right now, I have a text field that looks sorta like this:

<input type="text" name="name" value="" pattern=".{3,}" required title="3 char minimum">


Followed by a submit button:

<input type="submit" value="Submit" onclick="return confirm('Are you sure?')">


If there are not three characters in the text box and I click the submit button, the confirm() box will take priority of coming up first instead of the required title coming up first.

Is there a way to make the required title display before the confirmation window from the submit button?

Here's a fiddle: https://jsfiddle.net/yu21maor/

Answer

Instead of on click of submit button, you can put the call on the form submit.

Try This:

<form action="" onSubmit="return confirm('Are you sure?')">
  <input type="text" name="name" value="" pattern=".{3,}" required title="3 char minimum">
  <input type="submit" value="Submit">
</form>

Fiddle