Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my schema, how would I allow only specific email to register. For example, only people with an email address ending in gmail.com can register

import mongoose from "mongoose";



const UserSchema = new mongoose.Schema(
	{
		name: {
			type: String,
			required: true
		},
		email: {
			type: String,
			required: [true, "Please enter your email"],
			unique: true,
			
		},
		password: {
			type: String,
			required: true,
			minlength: 5,
		},

	},
	{ collection: 'user-data' }
)

export default mongoose.model('User', UserSchema)


What I have tried:

I have tried to use regex

'^[a-zA-Z0-9_.+-]+@(?:(?:[a-zA-Z0-9-]+\.)?[a-zA-Z]+\.)?(gmail)\.com$'
Posted
Updated 9-Jan-23 23:24pm
v2

1 solution

Try simplifying your regex:
RegEx
^[a-zA-Z0-9_.+-]+?@[a-zA-Z0-9-]+?gmail\.com$
If you are going to use regular expressions, you need a helper tool. Get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900