Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

javascript function to allow 2 or 3 numeric

Posted on 2010-09-21
7
296 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
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…

789 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