Validate Non-Blank and Numbers Only

[Total: 0    Average: 0/5]

This validates all the components of the form to ensure that only number values are entered, and that all components are not empty.

Body Code:

<form name=exf1 action="http://www.js-examples.com/">
<input type=text name=t1 value=""><BR>
<input type=text name=t2 value=""><BR>
<input type=button value="CHECK" onclick="check()">
</form>
<script>
function check() {
  var bad=false;
  if (document.exf1.t1.value == "" ||
      document.exf1.t2.value == "") {
	alert("You need to fill in the blanks!");
	bad=true;
  }
  if (!bad) {
    var a = document.exf1.t1.value.split("");
	for (var i in a) {
	  if (!a[i].match(/d/)) {
	    alert("Tisk1 Tisk1n"+a[i]+" is not a number!");
		bad=true;
	  }
	}
  }
  if (!bad) {
	var b = document.exf1.t2.value.split("");
	for (var i in b) {
	  if (b[i].match([0|1|2|3|4|5|6|7|8|9])) {
	    alert("Tisk2 Tisk2");
		bad=true;
	  }
	}
  }
  if (!bad) {
    alert("Submitting Data");
	//document.exf1.submit();
  }
}
</script>

Times Viewed: 0

Leave a Reply

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