Validate – Only Give 1 Re-Try

[Total: 0    Average: 0/5]

This shows how to have a form on the submit action determine if the data in the form is valid. It sets a counter and if the counter hits the limit a different alert is given and the user is sent to another page.

Body Code:

<form name=exf1 action="http://www.js-examples.com/js/">
Enter a "x" (no quotes):<input type=text name=t1>
<BR><input type=button value="Enter an 'x'?" onclick="doIt()">
</form>
<script>
var counter=0;
function doIt() {
  if (document.exf1.t1.value!="x") {
    if (++counter == 1) {
	  alert("Hmm... try again:");
	} else if (counter >= 2) {
	  alert("You only get 1 retry!nGo Learn Some JavaScript!");
	  document.location="http://www.js-examples.com/";
	}
  } else {
    alert("Submitted.");
    //document.exf1.submit();  // un-comment this to have it submit the form.
  }
}
</script>

Times Viewed: 0

Leave a Reply

Your email address will not be published. Required fields are marked *