Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q1802 How to make sure a user enters percentage correctly like 88.66 and not 8888 or 888.6..?

You are here: irt.org | FAQ | JavaScript | Number | Q1802 [ previous next ]

Try:

<HTML>
<HEAD>

<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>

<script language="javascript"><!--
function validate(Percent) {
  // Percent = document.frmPost.percent.value
  if (Percent == "") {
    alert("No value");
    return false;
  }
  if ((Percent.indexOf(".") == -1) && (Percent.length >= 3)) {
    alert("Invalid Percentage1");
    return false;
  }
  if ((Percent.indexOf(".")) == 4 || (Percent.indexOf(".")) == 3 || (Percent.indexOf(".")) == 0) {
    alert("Invalid Percentage2");
    return false;
  }
  if (isNaN(Percent)==true) {
    alert("Enter Numeric values");
    return false;
  }	
  return true;
}	
		
function validate1() {
  if (!(validate(document.frmPost.percent.value))) {
    document.frmPost.percent.focus();
    return false;
  }
  if (!(validate(document.frmPost.percent1.value))) {
    document.frmPost.percent1.focus();
    return false;
  }
  if (!(validate(document.frmPost.percent2.value))) {
    document.frmPost.percent2.focus();
    return false;
  }
  return true;
}
//--></script>
</HEAD>

<BODY>
<form name=frmPost onsubmit="return validate1();">
<input type=text name=percent size=10 maxlength=5><br>
<input type=text name=percent1 size=10 maxlength=5><br>
<input type=text name=percent2 size=10 maxlength=5><br>
<input type=submit value=submit>
</form>
<P>&nbsp;</P>

</BODY>
</HTML>

Submitted by Manivannan. T

©2018 Martin Webb