Solved

javascript function to allow 2 or 3 numeric

Posted on 2010-09-21
7
298 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
[X]
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
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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

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…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

707 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