2016-04-30 18 views
0

私は何が間違っているのか分かりませんが、宛先が空の場合は警告を出す必要があります。テキストボックスが空である場合

が、問題は#destの値をチェックするコードは到達しませんということである問題が

<html lang="en"> 
<head> 
<script type="text/javascript"> 
    function CheckFormPrac(form){ 

     var pcheck = document.getElementById("phone").value; 
     pattern = /^^02\d{8}$/; 

     if (!pattern.test(pcheck)) { 
      alert("Please input your number in format of 02########."); 
      return false; 
     } else { 
      return true; 
     } 

     var cdest = document.getElementById("dest").value; 
     if (cdest == ""){ 
      alert("Destination has not been filled."); 
      return false; 
     } else { 
      return true; 
     } 

    } 
</script> 
</head> 
<body> 
<form id="ffform" method="get" action="" 
Onsubmit="return CheckFormPrac(this);" > 
<fieldset> 
     Home Phone: 
    <input type="text" name="phone" id="phone" size=15 onblur="CheckFormPrac(this);" /> <br/> 

    What is your favorite destination to fly to? 
    <input type="text" name="dest" id="dest" size=30 onblur="CheckFormPrac(this);"/> <br/> 
    <input type="submit" value="Submit" /> 
    <input type="reset" value="Reset"/> 
</form> 

答えて

0

が何であるかを見つけるように見える傾けます。

if(!pattern.test(pcheck)) { 
    alert("Please input your number in format of 02########."); 
    return false; 
} else { 
    return true; 
} 

は、関数からの制御を常に返し、次のコードは実行されません。これからelseブロックを削除すると、コードは#destの値をチェックします。

イベントハンドラからはreturn true;が必要ないことに注意してください。これは、フォームのデフォルト動作であるためです。あなたの形で多少の誤差があり、また、あなたのonsubmitを呼び出しているか

function CheckFormPrac() { 
    var pcheck = document.getElementById("phone").value; 
    if(!/^02\d{8}$/.test(pcheck)) { 
     alert("Please input your number in format of 02########."); 
     return false; 
    } 

    if(document.getElementById("dest").value.trim() === "") { 
     alert("Destination has not been filled."); 
     return false; 
    } 
} 
+0

感謝せずに(マイナスアラート)これを行うことができ、私は本当のリターンの両方を削除しました。あなたが言ったように、それはイベントハンドラのデフォルトアクションです。 :)私はid = "dest"を入力した後に働いた:)ありがとう – josh

0

はここにいくつかの変更との完全なコードです。また、あなたはそのためのjavascript

https://jsfiddle.net/stevenkaspar/uoLrsz78/

<form method="get" action="/"> 
    Home Phone: 
    <input type="text" name="phone" pattern='(02)[0-9]{8}' required/> <br/> 

    What is your favorite destination to fly to? 
    <input type="text" name="dest" required/> <br/> 
    <input type="submit" value="Submit" /> 
    <input type="reset" value="Reset"/> 
</form> 
関連する問題