Form validation with javascript

Home » Tutorials » JavaScript » Form validation with javascript
In this lesson we’ll consider homework from last lesson and discuss, how to make form validation with javascript
Validation is process of checking valid data form users (online payment forms, registration forms and so on). I am sure, you understand why validation is very important. Invalid data can destroy your database for example. In this lesson I made very simple validation. You can use other ways.

Code lesson

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title></title>
	<link rel="stylesheet" href="">
</head>
<body>
	
<form action="" onsubmit="return validate();">
	<input type="text" id="userName">
	<input type="password" id="userPassword">

	<button type="submit">Отправить</button>
</form>

<script>
	
	function validate() {
		var userName = document.getElementById("userName");
		var userPassword = document.getElementById("userPassword");


		if(!userName.value) {
			userName.style.border = "2px solid red";
			return false;
		}

		if(!userPassword.value) {
			userPassword.style.border = "2px solid red";
			return false;
		}


		return true;

	}


</script>

</body>
</html>

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

Pin It on Pinterest

Share This