Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

login form validator #45

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions formValidator/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const form= document.querySelector('form');
const username= document.querySelector('#username');
const email= document.querySelector('#email');
const password= document.querySelector('#password');
const confirmPassword= document.querySelector('#confirm-password');

function showErr(input,message){
const formControl= input.parentElement;
formControl.className='form-control error';
const small = formControl. querySelector('small');
small.innerText= message;
}

function showSucc(input){
const formControl= input.parentElement;
formControl.className='form-control success';

}
function checkEmail(input){
const re=/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
;
if (re.test(input.value.trim())){
showSucc(input);
}else{
showErr(input,"Email is not valid")
}
}


function checkRequired (inputArr){

inputArr.forEach(function(input){
if(input.value.trim()===''){showErr(input,`${getFieldname(input)} is required`)}else{
showSucc(input)}
});
}
function getFieldname(input){
return input.id.charAt(0).toUpperCase()+input.id.slice(1);
}

function checkLength(input,min,max){
if(input.value.length< min){showErr(input,`${getFieldname(input)} must be at least ${min} characters`)}
else if(input.value.length>max){showErr(input,`${getFieldname(input)} must be less than ${max} characters`)}
else{ showSucc(input)}

}


function checkPasswordMatch(input1,input2){
if(input1.value!==input2.value){
showErr(input2,'Passwords do not match')
}
}

form.addEventListener('submit',function(evt){
evt.preventDefault();
checkRequired([username,email,password,confirmPassword]);

checkLength(username,3,15);
checkLength(password,6,25);
checkEmail(email);
checkPasswordMatch(password,confirmPassword);
})

43 changes: 43 additions & 0 deletions formValidator/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Validator </title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<form id="form" class="form">
<h2>Register With Us</h2>

<div class="form-control">
<label for="username">Username</label>
<input type="text" id="username" placeholder="Enter Username">
<small>Error Message</small>
</div>

<div class="form-control">
<label for="email">Email</label>
<input type="text" id="email" placeholder="Enter email">
<small>Error Message</small>
</div>
<div class="form-control">
<label for="password">Password</label>
<input type="password" id="password" placeholder="Enter password">
<small>Error Message</small>
</div>
<div class="form-control">
<label for="confirm-password">Confirm Password</label>
<input type="password" id="confirm-password" placeholder="Enter confirm-password">
<small>Error Message</small>
</div>
<button>Submit</button>
</form>

</div>

<script src="app.js"></script>
</body>
</html>
88 changes: 88 additions & 0 deletions formValidator/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
@import url('https://fonts.googleapis.com/css?family=Open+Sans&display=swap');


:root{
--success-color:#2ecc71;
--error-color:#e74c3c;
}




*{
box-sizing: border-box;
}

body{
background-color: #f9fafb;
font-family: 'Open Sans',sans-serif;
display: flex ;
align-items: center;
justify-content: center;
min-height: 100vh;
margin: 0;
}
.container{
background-color: white;
border-radius:5px ;
box-shadow:0 2px 10px rgba(0,0,0,0.3) ;
width: 446px;
}
#form{
padding:30px 40px;
}
h2{
text-align: center;
margin: 0 0 20px;
}
.form-control{
margin-bottom: 10px;
padding-bottom: 20px;
position: relative;
}
.form-control label{
color: #777;
display: block;
margin-bottom: 5px;
}
.form-control input{
border: 2px solid #f0f0f0;
border-radius: block;
width: 100%;
padding: 10px;
font-size: 14px;
}
.form-control input:focus{
outline: 0;
border-color:#777
}
.form-control.success input{
border-color: var(--success-color);
}
.form-control.error input{
border-color: var(--error-color);
}
.form-control small{
color:var(--error-color);
position: absolute;
bottom: 0;
left: 0;
visibility: hidden;
}

.form-control.error small{
visibility: visible;
}
.form button{
cursor: pointer;
background-color: #3498db;
border: 2px solid #3498db;
border-radius: 4px;
color: #fff;
display: block;
font-size: 16px;
padding: 10px;
margin-top:20px ;
width: 100%;

}