JavaScript Irish Mobile Regex

时间:2017-10-14 22:39:58

标签: javascript validation

我正在尝试验证表单,以检查用户是否已添加有效的爱尔兰移动设备,该移动设备应以083,085,086,087,089开头并且长度为十位数

我看了Validate an Irish mobile phone number,但我无法让它工作

我的支票功能

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

我尝试验证的地方

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

但即使有效号码,我也被告知该条目无效。我敢肯定我做错了什么但是我是正则表达式的新手而不是程序员所以不确定是什么。

为了回应Jayce444,我编辑了我的问题以包含完整的脚本,因为当我添加checkphone功能时,它会导致电子邮件和密码功能被忽略,并且提交的表单中包含无效值,例如test @test和p2代码密码

<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>

1 个答案:

答案 0 :(得分:2)

这个正则表达式有效:

08[3679]-\d{7}$

通过这个网站:https://regex101.com/可以帮助测试正则表达式,看看它们实际上在做什么。你的第二部分\d{0,10}说“匹配0到10位”。你想要的是在破折号后正好匹配7位数。