Jump to content

Javascript - Strip down txt inside boxes


tsilenzio

Recommended Posts

I have two textbox's that I want to strip free of anything but numbers before the form is submited.

 

<form name="stockinvest" method="get" action="index.php">
  <table cellpadding="0" cellspacing="5" border="0">
    <tr>
      <td>Starting Investment:</td>
      <td><input type="text" name="investment" value="$1,000,000"></td>
    </tr>
    <tr>
      <td>Daily Withdrawal:</td>
      <td><input type="text" name="withdrawl" value="0"></td>
    </tr>
    <tr>
      <td>Days to invest:</td>
      <td><input type="text" name="days" value="31"></td>
    </tr>
    <tr>
      <td colspan="2" align="center"><button type="submit" onclick="validate();">Calculate</button></td>
    </tr>
  </table>
</form>

 

I tried to fool around a little bit with javascript but im not too smart.. really i dont even have a validate() function :( .. Im not even sure if it will run the javascript code before it submits the page to the next PHP page.. =/

Link to comment
https://forums.phpfreaks.com/topic/120967-javascript-strip-down-txt-inside-boxes/
Share on other sites

This should get you started. I would add some more validation on the investment field as well

<html>
<head>
<script type="text/javascript">

function validate(formObj)
{
  //Remove non-numeric values
  stripNonNumeric(formObj['withdrawl']);
  stripNonNumeric(formObj['days']);

  //Ensure req field have value
  if (!formObj['investment'].value || !formObj['withdrawl'].value || !formObj['days'].value)
  {
    alert('All fields are required.');
    return false;
  }

  return true;
}

function stripNonNumeric(fieldObj)
{
    fieldObj.value = fieldObj.value.replace(/[^\d]/g, '');
}

</script>
</head>

<body>

<form name="stockinvest" onsubmit="return validate(this);" method="get" action="index.php">
  <table cellpadding="0" cellspacing="5" border="0">
    <tr>
      <td>Starting Investment:</td>
      <td><input type="text" name="investment" value="$1,000,000" /></td>
    </tr>
    <tr>
      <td>Daily Withdrawal:</td>
      <td><input type="text" name="withdrawl" value="0" /></td>
    </tr>
    <tr>
      <td>Days to invest:</td>
      <td><input type="text" name="days" value="31" /></td>
    </tr>
    <tr>
      <td colspan="2" align="center"><button type="submit">Calculate</button></td>
    </tr>
  </table>
</form>


</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.