tags:

views:

65

answers:

1

how to check input text before POST operation? it must be numbers only

<form method="post" action="credit.php">
Сумма кредита: <input type="text" name="sum"> <br />
первый взнос: <input type="text" name="pv"> <br />
Срок: <input type="text" name="srok"> <br />
Процентная ставка: <input type="text" name="percent"> <br />
<input type="submit">
</form>

sorry for my bad english.

+4  A: 

Use some kind of javascript..

<form method="post" action="credit.php" onsubmit="validateForm()">
<input type="text" name="pv" id="pv"> <br />
....
</form>


<script type="text/javascript">
function validateForm() {
  var result =  /^\d+$/.test(document.getElementById('pv').value);
  return result

}
</script>

If your onsubmit returns false, the form won't go. If it returns true, the form will submit.

bwawok
how to alert, when user's input is not a number?
butteff
and at input may be decimal (fraction). how to be with this?
butteff
How to alert? alert("Please enter a number"); return false;
bwawok
Make a regular expression to check what you want. A regular expression for decimal is ^\d*.?\d*$
bwawok
Please accept this answer if you are going to use it when the checkbox
bwawok