I'm doing some validation on a HTML form using min and max attributes. I also want to prevent form submission if the submit button is clicked once to prevent multiple form submissions before a page is reloaded.
As you said, you want to prevent form submission, so you must point your bullets to the <form>
tag. When you need to handle the submit
event, always attack the <form>
, so let's add an onsubmit
attribute:
<form method="post" action="my-script.php" onsubmit="return something();">
Now let's add a little funtion which tells to our <form>
what to do. In this case let's show the result from the input:
<script type="text/javascript">
function something() {
var v = document.getElementById( "endNo" ).value;
alert( "The value is " + v );
return false;
}
</script>
I added some id
attribute to the input to be handled by javascript:
<input type="number" id="endNo" name="endNo" placeholder="END" min="5" max="20">
The submit button only shows the disabled
event in the onclick
attribute because return false
is already being executed on the <form>
tag:
<input type="submit" name="name" value="REGISTER" onclick="this.disabled=true;">
The whole tag looks like this:
<script type="text/javascript">
function something() {
var v = document.getElementById( "endNo" ).value;
alert( "The value is " + v );
return false;
}
</script>
<form method="post" action="" onsubmit="return something();">
<input type="number" id="endNo" name="endNo" placeholder="END" min="5" max="20">
<input type="submit" name="name" value="REGISTER" onclick="this.disabled=true;">
</form>
See it in action: https://jsfiddle.net/darioferrer/mfs3adqm/
Check some similar cases: