Solved

error validating number 4 or 5 digits

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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-…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

948 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now