EkremG
EkremG

Reputation: 131

checking if text is entered. Could not find error

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>

Upvotes: 0

Views: 682

Answers (3)

Fabien M&#233;nager
Fabien M&#233;nager

Reputation: 140195

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>

Upvotes: 3

Kunal Vashist
Kunal Vashist

Reputation: 2471

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

Upvotes: 0

David Laberge
David Laberge

Reputation: 16061

Try

var x=document.getElementById("name").value;
if (x == '') { alert('message'); } // instead of null thanks @minmin

Upvotes: 2

Related Questions