Ejemplo 1: validación de inicio de sesión de JavaScript
<html>
<body>
<form id="form1" runat="server">
<div class="container">
<div class="main">
<h2>
Javascript Login Form Validation</h2>
<form id="form_id" method="post" name="myform">
<label>
User Name :</label>
<input type="text" name="username" id="username" />
<br><br>
<label>
Password :</label>
<input type="password" name="password" id="password" />
<input type="button" value="Login" id="submit" onclick="validate();" />
</form>
</div>
</div>
</form>
</html>
</body>
Ejemplo 2: validación de contraseña en javascript
var check = function() {
if (document.getElementById('password').value ==
document.getElementById('confirm_password').value) {
document.getElementById('message').style.color = 'green';
document.getElementById('message').innerHTML = 'matching';
} else {
document.getElementById('message').style.color = 'red';
document.getElementById('message').innerHTML = 'not matching';
}
}
let pattern = new RegExp("^(?=(.*[a-zA-Z]){1,})(?=(.*[0-9]){2,}).{8,}$"); //Regex: At least 8 characters with at least 2 numericals
let inputToListen = document.getElementById('pass-one'); // Get Input where psw is write
let valide = document.getElementsByClassName('indicator')[0]; //little indicator of validity of psw
inputToListen.addEventListener('input', function () { // Add event listener on input
if(pattern.test(inputToListen.value)){
valide.innerHTML = 'ok';
}else{
valide.innerHTML = 'not ok'
}
});
Ejemplo 3: validación de inicio de sesión de JavaScript
<script language = "JavaScript">
function validate() {
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
if (username == null || username == "") {
alert("Please enter the username.");
return false;
}
if (password == null || password == "") {
alert("Please enter the password.");
return false;
}
alert('Login successful');
}
</script>
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)