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
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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.