Reputation: 131
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
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
Reputation: 2471
The form attribute is missing in your code that why it doesn't work
Upvotes: 0
Reputation: 16061
Try
var x=document.getElementById("name").value;
if (x == '') { alert('message'); } // instead of null thanks @minmin
Upvotes: 2