2015-04-03 63 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