Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why this html javascript file doesnt work and how can i make user input to not be anything but positive number?

Ive tried few things and none of them works, it doesnt calculate the area, and i want to make user input to be positive number and send error if anything else is written

<!DOCTYPE html>
<html>

<head>

<title> Area of circle </title>

</head>

<body>
<script type="text/javascript">
<meta charset="UTF-8">

    function CalculateArea(){
    var r = document.form1.txtr.value;
    var area = (r * r * Math.PI);
    document.write("<P> Area is:" + area +"</P>")
}

</script>

<form name=form1>
    Type radient of circle:
    <input type="text" name="txtr" size=10>
    <br>
    <input type="button" value="Calculate" onClick='CalculateArea;'>
</form>
</body>
</html>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

first you need to add () to your function call. Second, it’s <p> not <P> and third you probably don’t want to use document.write.

function CalculateArea(){

    var r = document.getElementById('form1').value;
    let p = document.getElementById('area')
    var area = (r * r * Math.PI);
    if (r%1 !=0 || r < 1) p.innerHTML = 'Please enter a whole number greater than 0';
    else p.innerHTML = area;
}
<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">


<title> Area of circle </title>

</head>

<body>


<form id='form1'>
    Type radient of circle:
    <input type="text" name="txtr" size=10>
    <br>
    <input type="button" value="Calculate" onClick='CalculateArea()'>
    <p id='area'></p>
</form>
</body>
</html>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading