0

I made a simple little calculator for math class. It does simple things like find volumes and areas of certain shapes, but saves me a lot of time in homework.

I uploaded it to the internet for my classmates, but I figured I would make the forms only be able to have numbers in them. I found some answers on tizag, but I don't really understand those solutions.

I'm looking for something like this:

function calculation() {
    if (form.thenumbers.value = code to check if it is numeric)
    {
        calculations
    } else {
        alert("Numbers only please");
    }
}

If it can't be that simple, I just appreciate a little explanation to how any other way works. Thanks.

Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
Jack Davis
  • 605
  • 4
  • 11
  • 17
  • 5
    http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric – c69 Mar 11 '12 at 02:49

1 Answers1

1
 if (form.thenumbers.value.match(/^[\d]*$/)){
     //do stuff
 }

Should work for you. This will match the value for a 0 or more digits. If you would like it to match for 1 or more, use + in place of *.

Chris Sobolewski
  • 12,819
  • 12
  • 63
  • 96
  • Thanks, and just to be clear at the form.thenumbers part, I would put formname.name of specific form, and what would I do if it is one input without a whole form? – Jack Davis Mar 11 '12 at 02:57
  • If your form markup was `
    ` you could use `document.calculator.number.value` to get the value.
    – Chris Sobolewski Mar 11 '12 at 03:01