2017-12-15 12 views
0

私のフォームの1つを検証していますが、空であればそれがなくなり、境界線が赤に変わります。 bordercolorは同じままです。それが元の状態に戻ります正しく提出したらどこでそれを作るにはどうすればよい送信後にjavascriptで色を元に戻します

function convert() { 
 
\t var dAmount, currency, total, err = ""; 
 

 
\t dAmount = document.getElementById('dAmount').value; 
 
\t currency = document.getElementById('Currency').value; 
 

 
\t if (isNaN(dAmount)) { 
 
\t \t err += 'Enter a valid number!<br />'; 
 
\t \t document.getElementById('dAmount').style.borderColor = "red"; 
 
\t } 
 

 

 
\t if (dAmount == '') { 
 
\t \t err += 'Amount Missing!<br />' 
 
\t \t document.getElementById('dAmount').style.borderColor = "red"; 
 
\t } 
 

 

 
\t if (currency == 'Choose') { 
 
\t \t err += 'Please Select a currency!<br />'; 
 
\t \t document.getElementById('Currency').style.borderColor = "red"; 
 
\t \t return false; 
 
\t } else { 
 
\t \t document.getElementById('Choose').style.borderColor = 'gray'; 
 
\t } 
 

 

 
\t if (err.length > 0) { 
 
\t \t document.getElementById('error').style.display = 'block'; 
 
\t \t document.getElementById('error').innerHTML = err; 
 
\t \t return false; 
 
\t } else { 
 
\t \t document.getElementById('error').style.display = 'none'; 
 

 
\t } 
 

 

 

 

 

 

 
}
\t #wrapper { 
 
\t \t width: 600px; 
 
\t \t text-align: center; 
 
\t \t margin: 250px auto 0; 
 
\t } 
 

 
\t body { 
 

 
\t } 
 

 
\t form { 
 
\t \t width: 580px; 
 
\t \t padding: 10px; 
 
\t } 
 

 
\t label { 
 
\t \t float: left; 
 
\t \t width: 150px; 
 
\t \t display: block; 
 
\t \t clear: left; 
 
\t \t text-align: right; 
 
\t \t padding-right: 10px; 
 
\t \t margin-top: 15px; 
 
\t } 
 

 
\t input, select, span { 
 
\t \t margin-top: 15px; 
 
\t \t display: block; 
 
\t } 
 

 
\t input[type="button"] { 
 
\t \t float: right; 
 
\t } 
 

 
\t #error { 
 
\t \t border: 2px solid red; 
 
\t \t padding: 25px; 
 
\t \t text-align: center; 
 
\t \t font-size: 24px; 
 
\t \t font-weight: bold; 
 
\t \t display: none; 
 
\t }
<!DOCTYPE html> 
 

 
<html lang="en"> 
 

 
<head> 
 
\t <meta charset="UTF-8"> 
 
\t <title>Final Exam</title> 
 
\t <link rel="stylesheet" href="converter.css"> 
 
\t <script src="converter.js"></script> 
 
</head> 
 

 
<body> 
 
\t <div id="wrapper"> 
 
\t \t <h1> Currency Converter</h1> 
 
\t \t <div id="error"></div> 
 
\t \t <form id="CForm"> 
 
\t \t \t <label for="dAmount">Dollar Amount: </label> 
 
\t \t \t <input type="text" id="dAmount" name="dAmount" /> <br /> 
 

 
\t \t \t <label for="Currency"> Currency: </label> 
 
\t \t \t <select id="Currency" name="Currency"> 
 
\t \t \t \t <option value="Choose">Select One</option> 
 
\t \t \t \t <option value="British Pound"> British Pound </option> 
 
\t \t \t \t <option value="Mexican Peso"> Mexican Peso </option> 
 
\t \t \t \t <option value="Euro"> Euro </option> 
 
\t \t \t \t <option value="Japanese Yen"> Japanese Yen </option> 
 
\t \t \t </select> 
 
\t \t \t <br /> 
 
\t \t \t <label for="Total"> Amount</label> 
 
\t \t \t <span id="Total"></span> 
 
\t \t \t <br /> 
 

 
\t \t \t <input type="button" value="Convert" onclick="convert()" /> 
 
\t \t \t <input type="reset" \t value="Clear" id="bReset"/> 
 

 
\t \t \t </form> 
 
\t </div> 
 
</body> 
 

 
</html>
ここ

は私のコードです

+1

スタイルを直接設定する代わりに、新しいクラスを直接追加するのではなく、 '.red'と定義された赤い枠線。すべての入力が正しく入力されたら、これらの要素すべてを 'red'クラスから削除してください。 – panther

答えて

0

私はあなたが最初にそれ以外の部分を追加する必要があると思います2つのステートメントまたは単にelse ifステートメントを使用します。通貨に関しては、うまくいきますが、dAmountの場合、色が変更された後は変更していないので、赤色になります。

0

は、ブラウザがtype="number" attributerequired attribute、そして:invalid pseudo-classであなたのためにこれを処理してみましょう:

input:invalid, select:invalid { 
 
    border: 1px solid red; 
 
} 
 

 
form { 
 
\t width: 580px; 
 
\t padding: 10px; 
 
} 
 

 
label { 
 
\t float: left; 
 
\t width: 150px; 
 
\t display: block; 
 
\t clear: left; 
 
\t text-align: right; 
 
\t padding-right: 10px; 
 
\t margin-top: 15px; 
 
} 
 

 
input, select, span { 
 
\t margin-top: 15px; 
 
\t display: block; 
 
} 
 

 
input[type="button"] { 
 
\t float: right; 
 
}
<form id="CForm"> 
 
\t <label for="dAmount">Dollar Amount: </label> 
 
\t <input type="number" id="dAmount" name="dAmount" required /> <br /> 
 

 
\t <label for="Currency"> Currency: </label> 
 
\t <select id="Currency" name="Currency" required> 
 
\t \t <option value="" disabled selected hidden>Select One</option> 
 
\t \t <option value="British Pound"> British Pound </option> 
 
\t \t <option value="Mexican Peso"> Mexican Peso </option> 
 
\t \t <option value="Euro"> Euro </option> 
 
\t \t <option value="Japanese Yen"> Japanese Yen </option> 
 
\t </select> 
 
\t <br /> 
 
\t <label for="Total"> Amount</label> 
 
\t <span id="Total"></span> 
 
\t <br /> 
 

 
\t <input type="button" value="Convert" onclick="convert()" /> 
 
\t <input type="reset" \t value="Clear" id="bReset"/> 
 
</form>

selectoption魔法がthis SO question about select placeholdersから来ています。

MDN's article on form validationも参照してください。

関連する問題