<script language="JavaScript">
function chkname (){
var ip = document.getElementsById("user");
if(ip==""){
alert("please input something!");
return false;
}
}
</script>
<form name="" id="Editor" method="post" action="wrightchk.php" >
<input id="user" type="text" >
<input type="submit" onClick="chkname()">
</form>
When the user doesn’t input content, the form submits and triggers a redirect. I want it so that when no content is input, the form alerts, and does not redirect.
The variable
ipcontains a reference to the input element, you need to use thevalueproeprty to get it’s content, and the name of the method isgetElementById, notgetElementsById:To prevent the form to be posted when the validation fails, you need to return the result from the function in the event:
You need a
nameattribute on the input field in order for it to be included in the form data when posted: