Welcome to Dream.In.Code
Getting Help is Easy!

Join 131,606 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,175 people online right now. Registration is fast and FREE... Join Now!




number validation

 
Reply to this topicStart new topic

number validation

xcuezme
post 10 Oct, 2008 - 01:52 AM
Post #1


New D.I.C Head

*
Joined: 10 Oct, 2008
Posts: 1

i want a javascript which will check that the 1st and 4th entered no. should not be '0' or '1'.i have this script.

CODE
    function isValidPattern_Digits(str)
    {
        var regEx = new RegExp("[^0-9]","ig");
    
        return !regEx.test(str);
    }
        
if(phone == '' || !isValidPattern_Digits(phone))
        {
            alert("You have to input your phone number and it must have digits only.");
            document.frmRegistration.txtPhone.focus();
            return false;
        }


Mod edit - Added code tags.
User is offlineProfile CardPM

Go to the top of the page


William_Wilson
post 13 Oct, 2008 - 05:48 AM
Post #2


lost in compilation

Group Icon
Joined: 23 Dec, 2005
Posts: 3,951



Thanked 13 times

Dream Kudos: 3275

Expert In: Java, C, Javascript

My Contributions


I don't know why you are using a double negative in your function, but this will do what you need:
CODE

function isValidPattern_Digits(str)
{
        var regEx = new RegExp("^[2-9][0-9]{2}[2-9][0-9]*$","ig");

        return regEx.test(str);
}


it matches 1 occurance of 2-9, then 2 of 0-9 another 2-9 then countless 0-9 afterwards, thus the first and fourth positions cannot be 0 or 1.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 04:16AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month