简体   繁体   中英

Javascript: how to check if the input is correct

I know there is a way to make a field input and have user to type for example 'an apple' and then have javascript to check if the user typed in what I wanted which in this case is 'an apple' or if he/she typed in something else like 'a banana' and then if the answer was correct an information like 'correct' should appear and if it's not an information with 'wrong' should appear.

The question is how can I make it work?

I've searched the net for answers but it's giving me only how to check input length or something.

Can someone give me instructions or a link to a tutorial or something that would explain that, please.

Thank you.

try something like this

<input type="text" id="test">
<input type="button" value="click" onclick="z()"><span id="err"></span>

<script>
function z()
{
var a=document.getElementById("test");
if((a.value=="an apple")||(a.value=="apple"))
{
document.getElementById('err').innerHTML= 'correct';
}
else
{
document.getElementById('err').innerHTML= 'wrong';

}

}
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM