I am in the process of utilizing Javascript to validate an input field with the specific formatting requirements outlined below:
- "WORD1,WORD2"
The input must contain a comma separating two words, without any spaces. The first word (WORD1) can be any word, but the second word (WORD2) has to be one of the following options:
- "USD", "AUD", "BTC", "CAD", "CHF", "EUR", "GBP", "ETH", "JPY", "NZD"
If the input does not contain any of the designated words for WORD2, the validation will fail. For instance: "ASDA,USD" would pass validation, while "ASDA,ASD" would not.
How should I proceed with coding this? Here's what I've developed so far for uppercasing validation.
Javascript
function cryptoValidate() {
var cryptoBaseCurrencies = ("USD", "AUD", "BTC", "CAD", "CHF", "EUR", "GBP", "ETH", "JPY", "NZD");
let x = document.getElementById("inputText4").value;
let text;
if (x.toUpperCase() != x) {
document.getElementById('demo2').style.display = "block";
text = "Crypto and base must be uppercase";
document.getElementById("inputText4").value = '';
}
else if WORD FORMATTING HERE {
document.getElementById('demo2').style.display = "block";
text = "Missing the correct base currency"
document.getElementById("inputText4").value = '';
}
else {
text = "Input OK";
document.getElementById('demo2').style.display = "none";
}
document.getElementById("demo2").innerHTML = text;
}
HTML
<div class="col-auto">
<input type="text" id="inputText4" class="form-control" aria-describedby="TextHelpInline" placeholder="e.g. BTC,USD"/>
</div>
<div class="col-auto">
<button id="inputTextBtn4" class="btn set-btn" onclick="cryptoValidate()">Add</button>
</div>
<p id="demo2" style="display: none"></p>