Skip to content

Commit

Permalink
Merge pull request #124 from NandishM0618/formValid
Browse files Browse the repository at this point in the history
form validation implemented
  • Loading branch information
Kritika30032002 authored Dec 19, 2023
2 parents 4802f19 + 9a81ca8 commit 88bdd34
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 13 deletions.
24 changes: 19 additions & 5 deletions views/login.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,28 @@
<div class="form-group">
<label for="email">Email</label>

<input type="email" class="form-control" name="username" />
<input
type="email"
class="form-control"
name="username"
placeholder="Enter your email"
/>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" name="password" />
<input
type="password"
class="form-control"
name="password"
placeholder="Enter your password"
/>
</div>
<button type="submit" id="user-login" class="button_all_black" role="button">

<button
type="submit"
id="user-login"
class="button_all_black"
role="button"
>
Login
</button>
</form>
Expand Down Expand Up @@ -94,7 +108,7 @@
if (response.ok) {
const data = await response.json();
if (data.success) {
Toastify({
Toastify({
text: data.message,
type: "success",
}).showToast();
Expand Down
50 changes: 42 additions & 8 deletions views/register.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
type="email"
class="form-control"
name="username"
id="user-email"
/>
</div>
<div class="form-group">
Expand Down Expand Up @@ -93,12 +94,47 @@
},
});
function validatePassword() {
function validateForm(){
const email = document.getElementById("user-email").value;
const userPassword = document.getElementById("user-password").value;
var regex =
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
if (!regex.test(userPassword)) {
var regexEmail = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
if(!email && !userPassword){
Toastify({
text:"Please fill email and password",
type:"error",
style: {
background: "linear-gradient(to right, #4E575F,#343A40)",
},
duration:3000,
}).showToast();
return false;
}
else if(!email || !userPassword){
Toastify({
text:"Please fill email/password",
type:"error",
style: {
background: "linear-gradient(to right, #4E575F,#343A40)",
},
duration:3000,
}).showToast();
return false;
}
else if(!regexEmail.test(email)){
Toastify({
text: "Enter a valid Email",
type: "error",
style: {
background: "linear-gradient(to right, #4E575F,#343A40)",
},
duration: 3000,
}).showToast();
return false;
}
else if (!regex.test(userPassword)) {
Toastify({
text: "Password must have at least 8 characters, including one uppercase letter, one lowercase letter, one number, and one special character.",
type: "error",
Expand All @@ -109,18 +145,16 @@
}).showToast();
return false;
}
return true; // Return true when password is valid
return true;
}
const userRegister = document.getElementById("user-register");
userRegister.addEventListener("click", async (e) => {
e.preventDefault();
// Validate password
const isPasswordValid = validatePassword();
// Validate Form
const isFormValid = validateForm();
if (isPasswordValid) {
if (isFormValid) {
try {
// Send POST request only if the password is valid
const formData = JSON.stringify({
Expand Down

0 comments on commit 88bdd34

Please sign in to comment.