Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

error validating number 4 or 5 digits

Posted on 2010-11-16
4
Medium Priority
?
205 Views
Last Modified: 2012-05-10
I am hoping a javascript guru can help me debug a problem with a validation function.  I have a simple javascript function that validates an input that must be numeric and 4 or 5 digits.  However, it appears to be causing errors.

See below for the validation function.

Thanks for any help.
This code causes errors:

function checkemployeenumber(input) {
        var str=input.value;
        var filter = /^[0-9]{4,5}$/;

        if (filter.test(str)) {
            return true;
        }else{
            return false;
        }
    }

If I change to just validate 5 digits like this it works:

function checkemployeenumber(input) {
        var str=input.value;
        var filter = /^[0-9]{5}$/;

        if (filter.test(str)) {
            return true;
        }else{
            return false;
        }
    }

Open in new window

0
Comment
Question by:-Dman100-
[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
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
jrm213jrm213 earned 1000 total points
ID: 34149055
well that seems to work for me, but you could try

/\d{4,5}/
0
 
LVL 41

Assisted Solution

by:HonorGod
HonorGod earned 1000 total points
ID: 34149215
How are you using the function?

This appears to work just fine...
<html>
<head>
<script type='text/javascript'>
  function check( input ) {
    var str = input.value;
    var filter = /^[0-9]{4,5}$/;
    var result = filter.test( str );
    alert( result );
    return result;
  }
</script>
</head>
<body>
  Number: <input type=text onchange='check(this)'>
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:-Dman100-
ID: 34152371
Thanks for the help guys.  I determined it was another issue and not the javascript.  Thanks for your help!  I appreciate it.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 34154378
Thank you for the assist, and the points.

Good luck & have a great day.
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

604 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