Click here to Skip to main content
15,885,091 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need regular expression for mobile number which only allow 2 spaces and 11 digits number. ... Please reply me as soon as possible..

Like that

021 123 4567 is valid
0211234567 is valid
0211 234 567 is valid
Posted
Updated 14-Nov-14 0:30am
v2

Your examples did not follow your question. Look like your valid mobile number consists of 10 digits with or without 2 single spaces in between. Right, the try the sample code:
XML
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Validate...</button>
<script>
function myFunction() {

    var result = "Failed!";
    var isMatched = false;

    // First validate the pattern, ignoring digits count
    var mobileNum = "0211 234 567";
    var pattern = /^\d+\s?\d+\s?\d+$/g;
    var isMatched = pattern.test(mobileNum );

    // Next do digits count
    if (isMatched){
        var digits = mobileNum.replace(/ +/g, "");
        if(digits.length == 10){
            result = "Passed!";
        }
    }

    alert(result);
}
</script>

</body>
</html>
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900