最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript: how to check if the input is correct - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question asked Jun 30, 2014 at 17:28 user3791222user3791222 131 gold badge1 silver badge3 bronze badges 2
  • What exactly is your objective? Is it to check and see if the input is empty or not? Just an FYI what your trying to do is called input validation. – Demosthenes Commented Jun 30, 2014 at 17:30
  • There are three parts to your problem. One is getting the value from the field. The second is paring that value against your required field ("an apple"). The third is changing text on the screen. If you google these three separately you should have better luck. – wedstrom Commented Jun 30, 2014 at 17:37
Add a ment  | 

1 Answer 1

Reset to default 5

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>
发布评论

评论列表(0)

  1. 暂无评论