admin管理员组文章数量:1415093
Would like to know how to only accept number to be entered into inputbox from 1 to 9 and if entered for example 0 give alert message, sorry not a valid number.
please check my function that i have done so far, but not working.. thank you
<input name="number1" type="text" size="1" id="number1"onkeyup="doKeyUpValidation(this)/>
doKeyUpValidation(text){
var validationRegex = RegExp(/[-0-9]+/, "g");
if(!validationRegex.match(text.value)
{
alert('Please enter only numbers.');
}
Would like to know how to only accept number to be entered into inputbox from 1 to 9 and if entered for example 0 give alert message, sorry not a valid number.
please check my function that i have done so far, but not working.. thank you
<input name="number1" type="text" size="1" id="number1"onkeyup="doKeyUpValidation(this)/>
doKeyUpValidation(text){
var validationRegex = RegExp(/[-0-9]+/, "g");
if(!validationRegex.match(text.value)
{
alert('Please enter only numbers.');
}
Share
Improve this question
edited May 16, 2011 at 14:52
Quentin
945k133 gold badges1.3k silver badges1.4k bronze badges
asked May 16, 2011 at 14:48
user754443user754443
471 silver badge7 bronze badges
6
-
4
Seriously? You say
0-9
and expect it to reject0
?! Not to mention that the HTML is invalid and the JS is (at least) missing a brace. – Quentin Commented May 16, 2011 at 14:52 -
He says
[-0-9]
. Maybe s/he expected it to reject based on the leading-
, as if it were a 'not inclusive' syntax. Which it isn't; the regex was wrong. – Katie Kilian Commented May 16, 2011 at 14:57 - Yeah, voted the above ment up (from -1) - but also the question itself, to remove the -1. Let's not be TOO harsh, guys. – Mörre Commented May 16, 2011 at 14:57
- I wish people would vote based on "Is this question useful?" rather then "Does this question deserve to be considered (un)useful by that number of people?" (And remember that you get more points for an upvote then you lose for a downvote, so it isn't a balancing act) – Quentin Commented May 16, 2011 at 15:00
- 1 ...and by the way, PLEASE DON'T DO THIS. It is VERY bad style to issue a very intrusive modal "alert" box for such a minor data input problem. You should simply add a text (bold or red) next to the input box, but opening a modal dialog box is bad. One should not use "alert" and those other modal dialogs from the very early Javascript days of the 1990s at all any more, actually. – Mörre Commented May 16, 2011 at 15:02
3 Answers
Reset to default 1You're missing a closing quote at the end of your onkeyup
attribute, and as David mentions, you need to change your regex string to /[1-9]/
You were pretty close. Try this:
function doKeyUpValidation(text) {
var validationRegex = RegExp(/[1-9]+/, "g"); // The regex was wrong
if( !validationRegex.match(text.value) )
{
alert('Please enter only numbers.');
}
} // this was missing before
Your HTML is slightly wrong, it should be:
<input name="number1" type="text" size="1" id="number1" onkeyup="doKeyUpValidation(this)"/>
You need a space between each attribute, and each attribute needs to be quoted.
Also, your JavaScript has a few errors. It should be:
function doKeyUpValidation(text) {
var validationRegex = /[1-9]/g;
if (!validationRegex.test(text.value)) {
alert('Please enter only numbers.');
}
}
You need the function keyword to make doKeyUpValidation
a function. Also, your regex was a little off.
Demo: http://jsfiddle/EqhSS/10/
本文标签: inputOnly accept number from 1 to 9 Using JavaScriptStack Overflow
版权声明:本文标题:input - Only accept number from 1 to 9 Using JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745179935a2646419.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论