Solved

javascript function to allow 2 or 3 numeric

Posted on 2010-09-21
7
293 Views
Last Modified: 2012-06-21
Is there a java script function if I allowed 2 digit numbers and if I hit or added 3 digit numbers can i get a popup saying allow or disallow for example.
0
Comment
Question by:sevensnake77
7 Comments
 
LVL 4

Expert Comment

by:ronan1979
ID: 33729753
use the handler "onChange" and then check the length of the string
0
 
LVL 41

Accepted Solution

by:
HonorGod earned 500 total points
ID: 33730273
Something like this perhaps?
<html>
<head>
<script type='text/javascript'>
  function verify( obj ) {
    var val = obj.value;
    if ( /^\d{2}$/.test( val ) ) {
      alert( 'Good: "' + val + '"' );
    } else {
      alert( 'Invalid: "' + val + '"' );
    }
  }
</script>
</head>
<body>
2 digit number: <input type='text' onblur='verify(this)'>
</body>
</html>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 33730689
<< if I hit >>

I would replace line 15 of @HonorGod's code by :


2 digit number: <input type='text' onkeyup='verify(this)' onclick='verify(this)' >

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 22

Expert Comment

by:kadaba
ID: 33731448
If the idea is to disallow anything greater than 2 digit numbers, then simply add the max length property to the text box to 2 i.e maxlength = 2 and save user the trouble of entering more digits in the first place.

if you also need to make sure there are two digits compulsorily, Honorgod's example is the perfect one. I would not recommend leakims method for the reason it will annoy the user by showing alert every time anything is entered, at least I get annoyed.
0
 
LVL 9

Author Comment

by:sevensnake77
ID: 33734275
Understandable >> Kadaba

But I want it to ask me to allow the 3rd digit before actually implementing or showing it in the text box is that possible with Honorgod's method not to sure. And if not allow have a disallow kind of ok and cancel i can do that part.

 but the method to actually stop the user from adding 3 digits before displaying it.

So for example a user types 2 digit no problem then he hits the button again but before the digit goes a pop up ask yes or no what ever and if the user hit yes then the digit adds in. making it 3 digits.
0
 
LVL 9

Author Closing Comment

by:sevensnake77
ID: 33734326
tks
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 33734332
Thank you for the grade & points.

Good luck & have a great day.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This article discusses how to create an extensible mechanism for linked drop downs.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now