# PHP field validation

**URL:** <https://forum.kirupa.com/t/php-field-validation/636291>\
**Category:** programming\
**Created:** [March 24, 2017, 3:43pm UTC](https://forum.kirupa.com/t/php-field-validation/636291 "2017-03-24T15:43:13Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [March 27, 2017, 2:24am UTC](https://forum.kirupa.com/t/php-field-validation/636291/5 "2017-03-27T02:24:03Z")

</div>

For displaying the error on the same page, you can do that entirely in JavaScript and not have to deal with the server at all. The exact details will vary, but you can see one approach below:

```
<html>

<head>
	<title>Form Test</title>
</head>

<body>

	<div id="textContainer">
		<form action="#">
			<input id="nameField" type="text"></input>
			<input id="submitButton" type="Submit"></input>
		</form>
	</div>

	<script>
		var nameField = document.querySelector("#nameField");
		var submitButton = document.querySelector("#submitButton");

		submitButton.addEventListener("click", submitForm, false);

		function submitForm(e) {

			if (nameField.value == "") {
				alert("Value empty!");
				e.preventDefault();
			} else {
				// form submission will just happen
			}
		}
	</script>
</body>

</html>

```

Hope this helps 😛

---

_[View the full topic](https://forum.kirupa.com/t/php-field-validation/636291)._
