2017-10-14 66 views
0

我试图验证表单除其他事项外,以检查用户又增加了一个有效的爱尔兰移动应该有083,085,086,087,089开始,在长度为十位爱尔兰的JavaScript正则表达式移动

我看着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函数时,它会导致电子邮件和密码功能被忽略,并且表单被提交时存在这些字段的无效值,例如输入密码

<script> 

function validateEmail(email) { 
    var re = /^(([^<>()[\]\\.,;:\[email protected]\"]+(\.[^<>()[\]\\.,;:\[email protected]\"]+)*)|(\".+\"))@((\[[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> 

回答

2

此正则表达式的工作原理测试@测试和P2:

08[3679]-\d{7}$ 

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

+0

谢谢,但这打破了我的验证,它提出了检查电子邮件和密码功能失败。失败的脚本的完整代码。 我会尝试编辑我的问题,因为我不能在评论中发布代码 – Graham

+0

那么你是否说现在的电话号码验证工作,但电子邮件和密码验证不是? Cos是一个单独的问题 – Jayce444

+0

与正则表达式和检查代码就位,验证失败的密码和电子邮件。 我还没有测试只是正则表达式和手机检查代码,但稍后会这样做,并会确认 – Graham