-1
How can I insert a function that checks the optionlist
and after that checks the regexp of the banks below? There are 8 digits allowed in regexp to check the account in each bank, I do not know how to do this function.
function check(){
var verificaInput = document.querySelector("#agencia").value;
//Tá okay
var reg = /^[0-9]{4}$/;
if(reg.exec(verificaInput)){
//Se for igual a true
if(reg.test(verificaInput)){
//Seu código aqui
console.log("Passou");
}
}
else{
alert("Digite agência e conta novamente");
}
}
function changebancos(){
var selectedoption = document.getElementById("optionvalue");
//A coleção de option do seu select
var options = selectedoption.options;
//options.length: pega a quantidade
for(var i = 0; i < options.length;i++){
console.log(options[i].value);
}
}
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>test</title>
</head>
<body>
<label>
<select id="optionvalue" function="changebancos()" >
<option value= "0">Banco do Brasil</option>
<option value="1">Itaú</option>
<option value="2">Bradesco</option>
<option value="3">Santander</option>
<option value="4">Sorocred</option>
</select>
<br>
<input type="number" name="agencia" id="agencia"/>
<br>
<input type="number" name="conta" id="conta"/>
<br>
<input type="button" id="btnCheck"value="click to check" onclick="check(this.id)"/>
</label>
</body>
</html>
The latter console.log
which I placed is not returning anything on the console. I need it to return which option
was selected after clicking the button btnCheck
, and after that with a function check which bank was selected and check the Regexp checks.
It was not clear your question, could explain it better?
– LeAndrade
If you want to validate exactly 8 digits, use
/^[0-9]{8}$/
- the number between keys indicates the amount. And if you want a maximum of 8 digits, use/^[0-9]{0,8}$/
. And you don’t have to callexec
andtest
, call only one of them (if you just want to know if it’s valid, usetest
, whether it wants to extract information from match, useexec
) - read the documentation for the difference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp– hkotsubo