When a thread is closed, no replies can be made to it. Since you made a reply to this thread, and as have I, clearly the thread is not closed, which means that a misunderstanding has occurred somewhere.
Now, on to your form.
<FORM name="formx">
First, lowercase tags name please. While uppercase is allowed, it’s not acceptable these days.
For the rest of the form, there is no form field type called “number”. The closest you’ll get is “text”.
And please enclose attribute values in double quotes, and don’t forget to use an equals sign between the attribute name and its value. Thank you.
Before:
<input type "number" value=0 name="ans" size=9>
After:
<input type="text" value="0" name="ans" size="9">
Next, the name attribute on the form is performing a useless job. It just does nothing there. If you want a script to easily gain a reference to your form then you should use a unique identifier instead.
<form id="formx">
So far as sanitizing the form fields, it can be useful to sanitize them when they are entered, as as part of the onchange event of the field.
var form = document.getElementById('formx');
form.elements.a.onchange = sanitizeNumber;
form.elements.b.onchange = sanitizeNumber;
Now you can modify the entry as need be:
function sanitizeNumber() {
// restrict to digits only
this.value = /\\d+/.exec(this.value);
// size check
if (this.value < 1) {
this.value = 1;
}
if (this.value > 200) {
this.value = 200;
}
}
With your division button, get rid of the onclick event. That belongs to the early stone-age. Because you cannot target the button as a form field, you can apply a unique identifier to it instead.
<input id="divide" type="button" value=" / ">
Then use a similar technique as before to attach a function to the buttons event.
var divide = document.getElementById('divide');
divide.onclick = function () {
a_div_b(form);
}
Your a_div_b function can now be very simple indeed.
function a_div_b(form) {
var a = form.elements.a.value,
b = form.elements.b.value,
c = a / b;
form.elements.ans.value = c;
}
function sanitizeNumber() {
// restrict to digits only
this.value = /\\d+/.exec(this.value);
// size check
if (this.value < 1) {
this.value = 1;
}
if (this.value > 200) {
this.value = 200;
}
}
function a_div_b(form) {
var a = form.elements.a.value,
b = form.elements.b.value,
c = a / b;
form.elements.ans.value = c;
}
var form = document.getElementById('formx');
form.elements.a.onchange = sanitizeNumber;
form.elements.b.onchange = sanitizeNumber;
divide.onclick = function () {
a_div_b(form);
}
Here’s that script code all put together, which I will reiterate, needs to be placed at the end of the body, just before the </body> tag is the best place for the script.
function sanitizeNumber() {
// restrict to digits only
this.value = /\\d+/.exec(this.value);
// size check
if (this.value < 1) {
this.value = 1;
}
if (this.value > 200) {
this.value = 200;
}
}
function a_div_b(form) {
var a = form.elements.a.value,
b = form.elements.b.value,
c = a / b;
form.elements.ans.value = c;
}
var form = document.getElementById('formx');
form.elements.a.onchange = sanitizeNumber;
form.elements.b.onchange = sanitizeNumber;
divide.onclick = function () {
a_div_b(form);
}
So far as the error messages are concerned, you can update the answer field from the sanitizeNumber function, and return false to prevent the field from being left until the error is resolved.