Solved

error validating number 4 or 5 digits

Posted on 2010-11-16
4
199 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to deal with the query string 8 37
Html fieldset fix its height and width 4 32
Html Newline 7 21
JavaScript Sort Ordering and Choice Limiting 5 24
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

828 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