Solved

error validating number 4 or 5 digits

Posted on 2010-11-16
4
196 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
Comment Utility
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
Comment Utility
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-
Comment Utility
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
Comment Utility
Thank you for the assist, and the points.

Good luck & have a great day.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Form submit issue 11 52
CKEditor - will not function 7 30
Remove 'disabled' attribute 6 26
Cross domain request in Javescript 9 26
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

772 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

15 Experts available now in Live!

Get 1:1 Help Now