Solved

javascript function to allow 2 or 3 numeric

Posted on 2010-09-21
7
295 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Command prompt output to variable? 9 44
Converting dates in JavaScript 2 31
Passport Expiry 3 44
jQuery - following an example but can I save data server side? 20 46
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

816 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

15 Experts available now in Live!

Get 1:1 Help Now