Solved

error validating number 4 or 5 digits

Posted on 2010-11-16
4
198 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-
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
jrm213jrm213 earned 250 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 250 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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…

770 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