Skip to content

Commit

Permalink
add email validation with regex
Browse files Browse the repository at this point in the history
  • Loading branch information
Subas-mohanty committed Oct 22, 2024
1 parent 7734aa6 commit acb5214
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions apps/api/server/routes/api/users/index.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ export default defineEventHandler(async (event) => {
const username = user.username?.trim();
const password = user.password?.trim();
const {image, bio, demo} = user;
const regex = new RegExp("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$");

if (!email) {
throw new HttpException(422, {errors: {email: ["can't be blank"]}});
}

if(!regex.test(email)){
throw new HttpException(422, {errors : {email : ["must be a valid email address"]}});
}

if (!username) {
throw new HttpException(422, {errors: {username: ["can't be blank"]}});
}
Expand Down

0 comments on commit acb5214

Please sign in to comment.