0

I am trying to control if text is entered here is my code and it does not work i couldnt find whats wrong

<html>
<head>
    <script type="text/javascript">
        function CheckInfos()
        {
            var x=document.forms("name").value;
            if(x==null)
            {
                alert("adınızı kontrol ediniz");
            }
        }
    </script>   
</head>
<body>
    <input type="text" id="name" />
    <input type="button" value="test" onclick="CheckInfos()"/>
</body>

EkremG
  • 131
  • 1
  • 13

3 Answers3

3

Don't use "name" as an ID, but use getElementById.

var value = document.getElementById("foo").value;

But you should use a form :

<form name="myform" action="?" method="get" onsubmit="return CheckInfos(this)">
  <input type="text" name="foo" />
  <input type="submit" value="test"/>
</form>

<script>
function CheckInfos(form) {
  if (form.elements.foo.value == "") {
     alert("error");
  }

  return false;
}
</script>
Fabien Ménager
  • 140,109
  • 3
  • 41
  • 60
  • The reason why I discourage you to use IDs like "name" is because it may cause naming conflicts: http://stackoverflow.com/questions/9158238/why-js-function-name-conflicts-with-element-id . – Fabien Ménager Feb 29 '12 at 14:47
2

Try

var x=document.getElementById("name").value;
if (x == '') { alert('message'); } // instead of null thanks @minmin
David Laberge
  • 15,435
  • 14
  • 53
  • 83
0

The form attribute is missing in your code that why it doesn't work

Kunal Vashist
  • 2,380
  • 6
  • 30
  • 59