input - Only accept number from 1 to 9 Using JavaScript - Stack Overflow

Would like to know how to only accept number to be entered into inputbox from 1 to 9 and if entered for

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 reject 0?! 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
 |  Show 1 more ment

3 Answers 3

Reset to default 1

You'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/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745179935a4615372.html

相关推荐

  • input - Only accept number from 1 to 9 Using JavaScript - Stack Overflow

    Would like to know how to only accept number to be entered into inputbox from 1 to 9 and if entered for

    3小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信