0

Im trying to do a comparison with what the user type (his name - char) with the variable name (char). But idk how can i do that.

var nome=document.getElementById("nome").value;
var mat=document.getElementById("mat").value; //Disciplina

if (nome===name){
    alert("Sua média é: "+ resultado);  
} else {
    alert("Error.");
    return;
}

HTML Code:

<p class="p">Nome do aluno: <input class ="form-control" id="nome" type="text" name="name" placeholder="Preencha seu nome"/></p>
<button type="button" class="" onclick="media()">Calcular</button>

Javascript code:

function media() {
    var nome=document.getElementById("nome").value;
    var mat=document.getElementById("mat").value; //Disciplina

    if (nome===name){
        alert("Sua média é: "+ resultado);
    } else {
        alert("Error.");
        return;
    }
}
DaFois
  • 2,197
  • 8
  • 26
  • 43
Bruno
  • 1
  • 3
  • *Html*

    Nome do aluno:

    – Bruno Jun 01 '19 at 20:54
  • If I understand correctly, you want to run this comparison each time the user presses a button on the keyboard? – dejakob Jun 01 '19 at 20:58
  • No dekajob. Example: Your name is Dejakob. If you type Dekajob in my html site the program continue. But when u type a number, it shows a message "This is not a name". I want to do this comparison. – Bruno Jun 01 '19 at 21:01
  • Do you mean something like this? https://jsfiddle.net/mkqownc2/ – The fourth bird Jun 01 '19 at 21:05
  • The fourth bird, i want to do a comparison if your name is char or not. If your name is equal char (Right!) but if your name is equal a number - int - (Error!) – Bruno Jun 01 '19 at 21:08
  • When i say char is string. – Bruno Jun 01 '19 at 21:09
  • You might take a look at [this page](https://stackoverflow.com/questions/175739/built-in-way-in-javascript-to-check-if-a-string-is-a-valid-number) how you could do that – The fourth bird Jun 01 '19 at 21:18

1 Answers1

0

Do you mean you want to test if the input value is a valid name?Like this?

function testName(input) {
  return /^[a-zA-Z]+$/.test(input)
}

The regexp /^[a-zA-Z]+$/ only match strings combined by char a-z and A-Z.

In your code:

function media() {

    var nome=document.getElementById("nome").value;
    var mat=document.getElementById("mat").value; //Disciplina

    if (/^[a-zA-Z]+$/.test(nome)){
        alert("Sua média é: "+ resultado);

    }

    else{
        alert("Error.");
        return;
    }

}
BigLiao
  • 507
  • 3
  • 9
  • Yes BigLiao, but i started today learning js, i dont know hot to put this in the code. – Bruno Jun 01 '19 at 21:33
  • This is what i want to do, but with string. var num = "987238"; if(num.match(/^-{0,1}\d+$/)){ //valid integer (positive or negative) }else if(num.match(/^\d+\.\d+$/)){ //valid float }else{ //not valid number } – Bruno Jun 01 '19 at 21:39
  • But one more question, if i want to use special character (UTF-8). What is the regex? – Bruno Jun 01 '19 at 22:00
  • Yes you can use regexp to match special characters.For example, the chinese unicode set is "4E00-9FA5", so the regexp is `/[\u4E00-\u9FA5]/`. But this seems like a new feature, I'm not sure can it work in old browser. – BigLiao Jun 01 '19 at 22:22