Home > Software engineering >  Convert html input type number to actual number from html
Convert html input type number to actual number from html

Time:09-17

I have a html form which I am using a threshold for a python function. I managed to get the data as expected, but when I try to use the input for number it returns a string instead a float/number. I have been researching when I could use JS to do the conversion, still nothing really concrete found.

<form method="Post", id="myform">
    <input type="number" id="threshold" name="threshold" min=".1" max="100">
    <input type="submit" value="Submit">
</form>

app.route("/dp", methods=['GET', 'POST'])
def report():
    v = request.form.get("threshold")

    if request.method=='POST':
        //code

CodePudding user response:

Flask's form parser returns all data in string format regardless of how the input field itself is defined in your HTML; cast it to the datatype you need on the back-end in your Flask route:

@app.route("/dp", methods=['GET', 'POST'])
def report():
    v = float(request.form.get("threshold"))

    if request.method=='POST':
        //code

See How do I parse a string to a float or int? for further discussion on casting a string to int and float datatypes.

CodePudding user response:

In JS you can try parseInt. You can see the examples at : https://www.geeksforgeeks.org/convert-a-string-to-an-integer-in-javascript/

  • Related