684425 Posted July 17, 2015 Share Posted July 17, 2015 <input id="numb" type="number" /> <input type="submit" onclick="myFunction()" /> <p id="demo"></p> <script> function myFunction(){ var x = document.getElementById('numb').value; var regex=/^[1-9]+$/; if (x == ""){ document.getElementById('demo').innerHTML = "Input is Missing"; } else if (!x.match(regex)){ document.getElementById('demo').innerHTML = "Input Incorrect"; } else { document.getElementById('demo').innerHTML = "Input OK"; } } /* if (x == "") { document.getElementById('demo').innerHTML = "Input is Missing"; } else if (isNaN(x)) { document.getElementById('demo').innerHTML = "Input Incorrect"; } else { document.getElementById('demo').innerHTML = "Input OK"; } } */ </script> When input type is "text" it works fine. but when i change input type to "number" and then if i type letters or letters plus numbers. it says "input missing" How can i get correct output while using input type "number" ? Please guide. Thankyou. Quote Link to comment https://forums.phpfreaks.com/topic/297341-javascript-output-problem/ Share on other sites More sharing options...
scootstah Posted July 17, 2015 Share Posted July 17, 2015 type="number" only allows numbers to be entered. Quote Link to comment https://forums.phpfreaks.com/topic/297341-javascript-output-problem/#findComment-1516658 Share on other sites More sharing options...
684425 Posted July 17, 2015 Author Share Posted July 17, 2015 Plz check attached file. it does not give any error when letters are typed. new.html Quote Link to comment https://forums.phpfreaks.com/topic/297341-javascript-output-problem/#findComment-1516664 Share on other sites More sharing options...
scootstah Posted July 17, 2015 Share Posted July 17, 2015 There aren't errors, the value is just set to an empty string. Any non-numbers are considered invalid input and the whole thing just ends up being an empty string. Quote Link to comment https://forums.phpfreaks.com/topic/297341-javascript-output-problem/#findComment-1516667 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.