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.

1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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?
If your form markup was <form name="calculator"><input type='text' name='number'></form> you could use document.calculator.number.value to get the value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.