2015-04-03 58 views
0

开始我喜欢做,如果用户输入了不启动22或27或06等在JavaScript中的一个数字显示错误..显示错误,如果现场没有明确的数量

这里是我的代码..

<!DOCTYPE> 
<HTML> 
<form name="form" action="action.php" method="post" onsubmit="return validate()"> 
<span>Mobile Number(Required)</span> 
<input type ="integer" name="contact" id ="contact" 
value="+639" maxlength="13"/>&nbsp;<label id ="errorEight"></label> 
&nbsp;<label id ="errorAlphaFour"></label> 
&nbsp;<label id ="errorMinThree"></label><br> 
</HTML> 

<javascript> 
function validate() 
{ 
    var valid = true; 
    var numeric =/^[0-9+]+$/; 

    if(contact.value=="") 
    { 
     document.getElementById('errorEight').innerHTML="*Field is empty"; 
     document.getElementById('errorAlphaFour').innerHTML=""; 
     document.getElementById('errorMinThree').innerHTML=""; 
     valid=false; 
    } 
    else if(contact.value!="" && contact.value.match(numeric)) 
    { 
     document.getElementById('errorEight').innerHTML=""; 
     document.getElementById('errorAlphaFour').innerHTML=""; 
     document.getElementById('errorMinThree').innerHTML=""; 
    } 
    else 
    { 
     document.getElementById('errorAlphaFour').innerHTML="*Invalid number"; 
     document.getElementById('errorEight').innerHTML=""; 
     document.getElementById('errorMinThree').innerHTML=""; 
     valid = false; 
    } 

    if(contact!="" && contact.value.match(numeric) && contact.value.length<13) 
    { 
     document.getElementById('errorAlphaThree').innerHTML=""; 
     document.getElementById('errorTwo').innerHTML=""; 
     document.getElementById('errorMinThree').innerHTML="*Minimum of 13 digits"; 
     valid=false; 
    } 
    else 
    { 
    } 

    if(contact.value=="") 
    { 
     document.getElementById('errorEight').innerHTML="*Field is empty"; 
     valid = false; 
    } 
    else 
    { 
    } 
} 
</javascript> 

什么我需要添加到我的代码为它如果用户输入一个数字,不与27或22或06等

+0

'如果(value.indexOf('22' )=== 0)error' – adeneo 2015-04-03 14:31:16

+0

此:'如果(value.match(/ ^(2 [27] | 06)/ )){/ *错误的东西* /}' – Amessihel 2015-04-03 14:32:29

+0

谢谢,但它不工作.. – 2015-04-03 14:44:52

回答

0

你需要做的就是开始显示错误使用正则表达式执行简单的匹配测试。

var regex = '/^22[0-9]*$|27[0-9]*$|06[0-9]*$/g'; 
if (contact.match(regex) == null) 
{ 
    // Error display 
    // Instructions.... 
} 

希望它能帮助,

注意:如果你可以,请在发布前检查您的拼写和语法,就可以帮助我们来帮助你!

编辑:http://regexr.com/3aofd

+0

它不工作,但顺便说一句,谢谢。 – 2015-04-03 14:53:18

+0

是的,我的坏想法很快就做到了。我更新了新的正则表达式尝试;) – 2015-04-03 15:07:19