Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

javascript function to allow 2 or 3 numeric

Posted on 2010-09-21
7
Medium Priority
?
302 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:Seven price
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 2000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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:Seven price
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:Seven price
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
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.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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.
Suggested Courses

971 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