javascript - type="number" but still requires a text to number conversion -
i've element (x) of type=number. in work verify it's value number before performing mathematical operations i've concluded elements of type "number", while having builtin validation , helpful input controls, still assign values text , require conversion number before performing mathematical operations. question - true or there wrong in code? here's work. entered number 6.
x = document.createelement("input"); x.type = "number"; x.step = "0.1"; x.min = "0"; x.max = "9999"; x.defaultvalue = "1"; var p = document.getelementbyid(x.id).value; isthisnumber(p); function isthisnumber(z) { alert(typeof z + " --- " + typeof (z)); // returns "string --- string" if (z.isnan) { alert("it's not number"); } else { alert("it's number"); // returns false - it's number } if (z.tofixed) { alert("it's number"); } else { alert("it's not number"); // returns false - it's not number } var = z + z; alert("sum : " + a); // returns "66" var b = number(z); var b2 = b + b; alert("sum : " + b2); // returns "12" }
ref: https://developer.mozilla.org/en-us/docs/web/html/element/input/number
Comments
Post a Comment