Form Only Takes Numbers

[Total: 0    Average: 0/5]

This form will only take numbers.

Head Code:

<script>
/*
 * Pass this a string to determine if that string has only numbers in it
 * False returned if not
 * True returned if so
 */
function val(_str)
{
  alert(_str);
  /*
   * return value of FALSE stops the submit
   * TRUE allows the submit
   */

  /*
   * Match this pattern:
   * ^ beginning of line
   * d a digit (0,1,2..,9)
   * + at least one digit and many more
   * $ end of line
   */
  var _exp = new RegExp(/^d+$/);
  var _test=_exp.test(_str);
  alert(_test);
  return(_test);
}

function leaveDigits(_str)
{
  var STR="";
  var _exp =/d+/g;
  var _array;
  while(_array=_exp.exec(_str))
	STR+=_array.join();
  return(STR);
}
</script>

Body Code:

<form action='/' name=f1>
<input type=text name=t1 value="123asdf1234asdf">
<input type=button onclick="if(val(this.form.t1.value))this.form.submit()" value="submit">
<input type=button onclick="this.form.t1.value=leaveDigits(this.form.t1.value);" value="Leave Digits">
</form>

Times Viewed: 1

Leave a Reply

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