Graham
Graham

Reputation: 322

JavaScript Irish Mobile Regex

I'm trying to validate a form to check among other things that a user has added a valid Irish mobile which should start with 083,085,086,087,089 and be ten digits in length

I looked at Validate an Irish mobile phone number but I can't get it to work

My check function

function CheckPhone(pNumb) {   
    var paswd=/08[3679]-\d{0,10}$/; 
    return paswd.test(pNumb);}

Where I try to validate

if(!CheckPhone(d))
        {
            alert("You have not entered a valid Irish mobile number");
            document.getElementById('phoneNumber').focus();
            return false;    
        }

But even with a valid number I'm told that the entry is invalid. I'm sure I've done something wrong but I'm new to regex and not a programmer so not sure what.

In response to Jayce444 I've edited my question to include the full script as when I add the checkphone function it causes the email and password functions to be ignored and the form is submitted with invalid values present for these fields such as test@test and p2 for the password

<script>

function validateEmail(email) {
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}
function CheckPassword(inputtxt) {   
    var paswd=/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,20}$/; 
    return paswd.test(inputtxt);
}
function CheckPhone(pNumb) {   
    var paswd=08[3679]-\d{7}$; 
    return paswd.test(pNumb);
}
function validateForm() {
    var a=document.forms["subForm"]["firstName"].value;
    if (a===null || a==="")
        {
            alert("All fields in the form are required You must enter your first name");
            document.getElementById('firstName').focus();
            return false;
        }
    var b=document.forms["subForm"]["lastName"].value;
     if (b===null || b==="")
        {
            alert("All fields in the form are required You must enter your last name");
            document.getElementById('lastName').focus();
            return false;
        }
    var c=document.forms["subForm"]["email"].value;
     if (c===null || c==="")
        {
            alert("All fields in the form are required You must enter your email address");
            document.getElementById('email').focus();
            return false;
        }
      if(validateEmail(c)===false)
       {
            alert("You must enter a valid email address");
            document.getElementById('email').focus();
            return false;      
       }
      var d=document.forms["subForm"]["phoneNumber"].value;  
       if (d===null || d==="")
        {
            alert("All fields in the form are required You must enter your phone number");
            document.getElementById('phoneNumber').focus();
            return false;
        }
       if(CheckPhone(d)===false)
        {
            alert("You have not entered a valid Irish mobile number");
            document.getElementById('phoneNumber').focus();
            return false;    
        } 
      var e=document.forms["subForm"]["password"].value;  
       if (e===null || e==="")
        {
            alert("All fields in the form are required You must enter a password");
            document.getElementById('password').focus();
            return false;
        }    
       if(CheckPassword(e)===false)
        {
            alert("You have not entered a valid password");
            document.getElementById('password').focus();
            return false;    
        }

}
</script>

Upvotes: 0

Views: 630

Answers (1)

Jayce444
Jayce444

Reputation: 9063

This regex works:

08[3679]-\d{7}$

Btw this site: https://regex101.com/ is good to help test regexes and see what they're actually doing. The second part of yours, \d{0,10} says "match 0 to 10 digits". What you want is to match exactly 7 digits after the dash.

Upvotes: 1

Related Questions